Read In Files and Clean

votes = read.csv("C:/Users/onest/desktop/2012-and-2016-presidential-elections/votes.csv", header=T)

religion = read.csv("C:/Users/onest/desktop/2012-and-2016-presidential-elections/religion.csv", header=T)
religions = religion[,c(1,43,44,45,46,47,48,49,50,51,52,53,54,55)]
colnames(religions) = c("FIPS", "Total_Pop", "Evangelical", "Protestant", "Historically_Black", "Catholic", "Jewish", "Mormon", "Islamic", "Hindu", "Buddhist", "Orthodox", "Jehovas_Witnesses", "Other_Religion")

votes = merge(x=votes,y=religions,by="FIPS", all.x=T)

Exploratory Analaysis - Maps

Basics

colnames(votes)[c(29:31,33,36:39,41:43,47:54,56,58:75)] = c("Population 2012",
                                                         "Persons Under 5", 
                                                         "Persons Under 18",
                                                         "% Female 2014",
                                                         "Indian and Alaskan Native",
                                                         "Asian",
                                                         "Native Hawaiian",
                                                         "2+ Races",
                                                         "White",
                                                         "Living in Same House 1+ Years",
                                                         "Foreign Born",
                                                         "Veterans",
                                                         "Travel Time to Work",
                                                         "Housing Units 2014",
                                                         "Homeownership Rate",
                                                         "Housing Units in Multi-Unit Structures",
                                                         "Median Value of Owner-Occupied Housing Units",
                                                         "Households",
                                                         "Persons/Household",
                                                         "Median Household Income",
                                                         "Private Nonfarm Establishments 2013",
                                                         "Private Nonfarm Employment",
                                                         "% Change - Private Nonfarm Employment",
                                                         "Nonemployer Establishments - 2013",
                                                         "Total Number of Firms",
                                                         "Black-Owned Firms",
                                                         "Indidan and Alaskan -Owned Firms",
                                                         "Asian-Owned Firms",
                                                         "Hawaiian-Owned Firms",
                                                         "Hispanic-Owned Firms",
                                                         "Women", 
                                                         "Manufacturers Shipments - 2007",
                                                         "Merchant Wholesaler Sales - 2007",
                                                         "Retail Sales - 2007",
                                                         "Retail Sales / Capita - 2007",
                                                         "Accommodation and Food Service Sales - 2007",
                                                         "Building Permits",
                                                         "Land Area (in sq miles)")
colnames(votes)
##  [1] "FIPS"                                        
##  [2] "X.1"                                         
##  [3] "X"                                           
##  [4] "combined_fips"                               
##  [5] "votes_dem_2016"                              
##  [6] "votes_gop_2016"                              
##  [7] "total_votes_2016"                            
##  [8] "Clinton"                                     
##  [9] "Trump"                                       
## [10] "diff_2016"                                   
## [11] "per_point_diff_2016"                         
## [12] "state_abbr"                                  
## [13] "county_name"                                 
## [14] "total_votes_2012"                            
## [15] "votes_dem_2012"                              
## [16] "votes_gop_2012"                              
## [17] "county_fips"                                 
## [18] "state_fips"                                  
## [19] "Obama"                                       
## [20] "Romney"                                      
## [21] "diff_2012"                                   
## [22] "per_point_diff_2012"                         
## [23] "fips"                                        
## [24] "area_name"                                   
## [25] "state_abbreviation"                          
## [26] "population2014"                              
## [27] "population2010"                              
## [28] "population_change"                           
## [29] "Population 2012"                             
## [30] "Persons Under 5"                             
## [31] "Persons Under 18"                            
## [32] "age65plus"                                   
## [33] "% Female 2014"                               
## [34] "White"                                       
## [35] "Black"                                       
## [36] "Indian and Alaskan Native"                   
## [37] "Asian"                                       
## [38] "Native Hawaiian"                             
## [39] "2+ Races"                                    
## [40] "Hispanic"                                    
## [41] "White"                                       
## [42] "Living in Same House 1+ Years"               
## [43] "Foreign Born"                                
## [44] "NonEnglish"                                  
## [45] "Edu_highschool"                              
## [46] "Edu_batchelors"                              
## [47] "Veterans"                                    
## [48] "Travel Time to Work"                         
## [49] "Housing Units 2014"                          
## [50] "Homeownership Rate"                          
## [51] "Housing Units in Multi-Unit Structures"      
## [52] "Median Value of Owner-Occupied Housing Units"
## [53] "Households"                                  
## [54] "Persons/Household"                           
## [55] "Income"                                      
## [56] "Median Household Income"                     
## [57] "Poverty"                                     
## [58] "Private Nonfarm Establishments 2013"         
## [59] "Private Nonfarm Employment"                  
## [60] "% Change - Private Nonfarm Employment"       
## [61] "Nonemployer Establishments - 2013"           
## [62] "Total Number of Firms"                       
## [63] "Black-Owned Firms"                           
## [64] "Indidan and Alaskan -Owned Firms"            
## [65] "Asian-Owned Firms"                           
## [66] "Hawaiian-Owned Firms"                        
## [67] "Hispanic-Owned Firms"                        
## [68] "Women"                                       
## [69] "Manufacturers Shipments - 2007"              
## [70] "Merchant Wholesaler Sales - 2007"            
## [71] "Retail Sales - 2007"                         
## [72] "Retail Sales / Capita - 2007"                
## [73] "Accommodation and Food Service Sales - 2007" 
## [74] "Building Permits"                            
## [75] "Land Area (in sq miles)"                     
## [76] "Density"                                     
## [77] "Clinton_Obama"                               
## [78] "Trump_Romney"                                
## [79] "Trump_Prediction"                            
## [80] "Clinton_Prediction"                          
## [81] "Trump_Deviation"                             
## [82] "Clinton_Deviation"                           
## [83] "Total_Pop"                                   
## [84] "Evangelical"                                 
## [85] "Protestant"                                  
## [86] "Historically_Black"                          
## [87] "Catholic"                                    
## [88] "Jewish"                                      
## [89] "Mormon"                                      
## [90] "Islamic"                                     
## [91] "Hindu"                                       
## [92] "Buddhist"                                    
## [93] "Orthodox"                                    
## [94] "Jehovas_Witnesses"                           
## [95] "Other_Religion"

Vote Share Plots

library("choroplethr")
## Warning: package 'choroplethr' was built under R version 3.3.3
## Loading required package: acs
## Warning: package 'acs' was built under R version 3.3.3
## Loading required package: stringr
## Loading required package: plyr
## Warning: package 'plyr' was built under R version 3.3.3
## Loading required package: XML
## Warning: package 'XML' was built under R version 3.3.3
## 
## Attaching package: 'acs'
## The following object is masked from 'package:base':
## 
##     apply
library("choroplethrMaps")
## Warning: package 'choroplethrMaps' was built under R version 3.3.3
library("ggplot2")
## Warning: package 'ggplot2' was built under R version 3.3.3
#Explore total votes per county
total_votes = votes[,c(1,7)]
colnames(total_votes) = c('region','value')

vote_count = county_choropleth(total_votes,
                               legend = "Votes",
                               num_colors = 9,
                               title = "Vote Count by County")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 2180, 2188, 2240, 2090, 2198,
## 15005, 2100, 2170, 51515, 2016, 2060, 2290, 2282, 2070, 2110, 2130, 2185,
## 2195, 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
vote_count

#Break down county vote into regions of the US for easier viewing

##New England Region
vote_count_NE = county_choropleth(total_votes,
                               legend = "Votes",
                               num_colors = 9,
                               title = "Vote Count by County - New England",
                               state_zoom = c("maine","new hampshire", "vermont", "massachusetts","connecticut","rhode island"))
vote_count_NE

##Mid-Atlantic Region
vote_count_MA = county_choropleth(total_votes,
                               legend = "Votes",
                               num_colors = 9,
                               title = "Vote Count by County - Mid-Atlantic",
                               state_zoom = c("new york", "pennsylvania", "new jersey", "maryland","delaware"))
vote_count_MA

##South East Region
vote_count_SE = county_choropleth(total_votes,
                               legend = "Votes",
                               num_colors = 9,
                               title = "Vote Count by County - South East",
                               state_zoom = c("west virginia","virginia","tennessee","kentucky","north carolina","south carolina","georgia","florida","arkansas","mississippi","alabama","louisiana"))
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 51515
vote_count_SE

##Mid West Region
vote_count_MW = county_choropleth(total_votes,
                               legend = "Votes",
                               num_colors = 9,
                               title = "Vote Count by County - Mid-West",
                               state_zoom = c("ohio","michigan","indiana","illinois","wisconsin","minnesota","iowa","missouri","north dakota","south dakota","nebraska","kansas"))
vote_count_MW

##South West Region
vote_count_SW = county_choropleth(total_votes,
                               legend = "Votes",
                               num_colors = 9,
                               title = "Vote Count by County - South West",
                               state_zoom = c("texas","oklahoma","new mexico","arizona"))
vote_count_SW

##West Region
vote_count_W = county_choropleth(total_votes,
                               legend = "Votes",
                               num_colors = 9,
                               title = "Vote Count by County - West",
                               state_zoom = c("colorado","wyoming","montana","idaho","utah","nevada","california","oregon","washington","alaska","hawaii"))
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 2180, 2188, 2240, 2090, 2198,
## 15005, 2100, 2170, 2016, 2060, 2290, 2282, 2070, 2110, 2130, 2185, 2195,
## 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
vote_count_W

#Explore Vote Count by Swing States
vote_count_swing = county_choropleth(total_votes,
                                     legend = "Votes",
                                     num_colors = 9,
                                     title = "Vote Count by County - Swing States",
                                     state_zoom = c("new hampshire","pennsylvania","ohio","michigan","north carolina","florida","arizona","iowa","nevada","wisconsin","virginia","colorado","minnesota","maine"))
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 51515
vote_count_swing

Obama to Clinton (2012 to 2016 Change)

votes$change_dem_votes = votes$votes_dem_2016 - votes$votes_dem_2012

dem_votes = votes[,c(1,96)]
dem_votes[,3] = NA
colnames(dem_votes) = c('region','votes','value')

for(i in seq(1:dim(dem_votes)[1])){
  if(dem_votes[i,2] > 0 && dem_votes[i,2] < 1000){
    dem_votes[i,3] = "Gain - Small"
  } else if(dem_votes[i,2] > 1000 && dem_votes[i,2] < 10000){
    dem_votes[i,3] = "Gain - Considerable"
  } else if(dem_votes[i,2] >= 10000){
    dem_votes[i,3] = "Gain - Large"
  } else if(dem_votes[i,2] < 0 && dem_votes[i,2] > -1000) {
    dem_votes[i,3] = "Loss - Small"
  } else if(dem_votes[i,2] < -1000 && dem_votes[i,2] > -10000){
    dem_votes[i,3] = "Loss - Considerable"
  } else if(dem_votes[i,2] < -10000){
    dem_votes[i,3] = "Loss - Large"
  } else{
  dem_votes[i,3] = "Equal"
  }
}

c = CountyChoropleth$new(dem_votes)
c$title = "Change from Obama to Clinton where Blue Represents better Clinton Performance"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(7)
c$ggplot_scale = scale_fill_manual(values = c("white", "blue","navy","deepskyblue","red","darkred","lightpink"))
dem_change_US = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 2180, 2188, 2240, 2090, 2198,
## 15005, 2100, 2170, 51515, 2016, 2060, 2290, 2282, 2070, 2110, 2130, 2185,
## 2195, 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
dem_change_US

#Break down county vote into regions of the US for easier viewing

##New England Region
c = CountyChoropleth$new(dem_votes)
c$title = "Change from Obama to Clinton (Blue = Better Clinton) - New England"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("blue","navy","deepskyblue","red","darkred","lightpink"))
c$set_zoom(c("maine","new hampshire", "vermont", "massachusetts","connecticut","rhode island"))
dem_change_NE = c$render() + 
              theme(legend.position = "right")
dem_change_NE

##Mid-Atlantic Region
c = CountyChoropleth$new(dem_votes)
c$title = "Change from Obama to Clinton (Blue = Better Clinton) - Mid-Atlantic"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("blue","navy","deepskyblue","red","darkred","lightpink"))
c$set_zoom(c("new york", "pennsylvania", "new jersey", "maryland","delaware"))
dem_change_MA = c$render() + 
              theme(legend.position = "right")
dem_change_MA

##South East Region
c = CountyChoropleth$new(dem_votes)
c$title = "Change from Obama to Clinton (Blue = Better Clinton) - South East"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(7)
c$ggplot_scale = scale_fill_manual(values = c("white","blue","navy","deepskyblue","red","darkred","lightpink"))
c$set_zoom(c("west virginia","virginia","tennessee","kentucky","north carolina","south carolina","georgia","florida","arkansas","mississippi","alabama","louisiana"))
dem_change_SE = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 51515
dem_change_SE

##Mid West Region
c = CountyChoropleth$new(dem_votes)
c$title = "Change from Obama to Clinton (Blue = Better Clinton) - Mid West"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("blue","navy","deepskyblue","red","darkred","lightpink"))
c$set_zoom(c("ohio","michigan","indiana","illinois","wisconsin","minnesota","iowa","missouri","north dakota","south dakota","nebraska","kansas"))
dem_change_MW = c$render() + 
              theme(legend.position = "right")
dem_change_MW

##South West Region
c = CountyChoropleth$new(dem_votes)
c$title = "Change from Obama to Clinton (Blue = Better Clinton) - South West"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(7)
c$ggplot_scale = scale_fill_manual(values = c("white","blue","navy","deepskyblue","red","darkred","lightpink"))
c$set_zoom(c("texas","oklahoma","new mexico","arizona"))
dem_change_SW = c$render() + 
              theme(legend.position = "right")
dem_change_SW

##West Region
c = CountyChoropleth$new(dem_votes)
c$title = "Change from Obama to Clinton (Blue = Better Clinton) - West"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(7)
c$ggplot_scale = scale_fill_manual(values = c("white","blue","navy","deepskyblue","red","darkred","lightpink"))
c$set_zoom(c("colorado","wyoming","montana","idaho","utah","nevada","california","oregon","washington","alaska","hawaii"))
dem_change_W = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 2180, 2188, 2240, 2090, 2198,
## 15005, 2100, 2170, 2016, 2060, 2290, 2282, 2070, 2110, 2130, 2185, 2195,
## 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
dem_change_W

#Explore Vote Count by Swing States
c = CountyChoropleth$new(dem_votes)
c$title = "Change from Obama to Clinton (Blue = Better Clinton) - Swing States"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("blue","navy","deepskyblue","red","darkred","lightpink"))
c$set_zoom(c("new hampshire","pennsylvania","ohio","michigan","north carolina","florida","arizona","iowa","nevada","wisconsin","virginia","colorado","minnesota","maine"))
dem_change_swing = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 51515
dem_change_swing

Romney to Trump (2012 to 2016 Change)

votes$change_rep_votes = votes$votes_gop_2016 - votes$votes_gop_2012

rep_votes = votes[,c(1,97)]
rep_votes[,3] = NA
colnames(rep_votes) = c('region','votes','value')

for(i in seq(1:dim(rep_votes)[1])){
  if(rep_votes[i,2] > 0 && rep_votes[i,2] < 1000){
    rep_votes[i,3] = "Gain - Small"
  } else if(rep_votes[i,2] > 1000 && rep_votes[i,2] < 10000){
    rep_votes[i,3] = "Gain - Considerable"
  } else if(rep_votes[i,2] >= 10000){
    rep_votes[i,3] = "Gain - Large"
  } else if(rep_votes[i,2] < 0 && rep_votes[i,2] > -1000) {
    rep_votes[i,3] = "Loss - Small"
  } else if(rep_votes[i,2] < -1000 && rep_votes[i,2] > -10000){
    rep_votes[i,3] = "Loss - Considerable"
  } else if(rep_votes[i,2] < -10000){
    rep_votes[i,3] = "Loss - Large"
  } else{
  rep_votes[i,3] = "Equal"
  }
}

c = CountyChoropleth$new(rep_votes)
c$title = "Change from Romney to Trump where Red Represents better Trump Performance"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(7)
c$ggplot_scale = scale_fill_manual(values = c("white","red","darkred","lightpink","blue","navy","deepskyblue"))
rep_change_US = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 2180, 2188, 2240, 2090, 2198,
## 15005, 2100, 2170, 51515, 2016, 2060, 2290, 2282, 2070, 2110, 2130, 2185,
## 2195, 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
rep_change_US

#Break down county vote into regions of the US for easier viewing

##New England Region
c = CountyChoropleth$new(rep_votes)
c$title = "Change from Romney to Trump (Red = Better Trump) - New England"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("red","darkred","lightpink","blue","navy","deepskyblue"))
c$set_zoom(c("maine","new hampshire", "vermont", "massachusetts","connecticut","rhode island"))
rep_change_NE = c$render() + 
              theme(legend.position = "right")
rep_change_NE

##Mid-Atlantic Region
c = CountyChoropleth$new(rep_votes)
c$title = "Change from Romney to Trump (Red = Better Trump) - Mid-Atlantic"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("red","darkred","lightpink","blue","navy","deepskyblue"))
c$set_zoom(c("new york", "pennsylvania", "new jersey", "maryland","delaware"))
rep_change_MA = c$render() + 
              theme(legend.position = "right")
rep_change_MA

##South East Region
c = CountyChoropleth$new(rep_votes)
c$title = "Change from Romney to Trump (Red = Better Trump) - South East"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("red","darkred","lightpink","blue","navy","deepskyblue"))
c$set_zoom(c("west virginia","virginia","tennessee","kentucky","north carolina","south carolina","georgia","florida","arkansas","mississippi","alabama","louisiana"))
rep_change_SE = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 51515
rep_change_SE

##Mid West Region
c = CountyChoropleth$new(rep_votes)
c$title = "Change from Romney to Trump (Red = Better Trump) - Mid West"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("red","darkred","lightpink","blue","navy","deepskyblue"))
c$set_zoom(c("ohio","michigan","indiana","illinois","wisconsin","minnesota","iowa","missouri","north dakota","south dakota","nebraska","kansas"))
rep_change_MW = c$render() + 
              theme(legend.position = "right")
rep_change_MW

##South West Region
c = CountyChoropleth$new(rep_votes)
c$title = "Change from Romney to Trump (Red = Better Trump) - South West"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("red","darkred","lightpink","blue","navy","deepskyblue"))
c$set_zoom(c("texas","oklahoma","new mexico","arizona"))
rep_change_SW = c$render() + 
              theme(legend.position = "right")
rep_change_SW

##West Region
c = CountyChoropleth$new(rep_votes)
c$title = "Change from Romney to Trump (Red = Better Trump) - West"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(7)
c$ggplot_scale = scale_fill_manual(values = c("white","red","darkred","lightpink","blue","navy","deepskyblue"))
c$set_zoom(c("colorado","wyoming","montana","idaho","utah","nevada","california","oregon","washington","alaska","hawaii"))
rep_change_W = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 2180, 2188, 2240, 2090, 2198,
## 15005, 2100, 2170, 2016, 2060, 2290, 2282, 2070, 2110, 2130, 2185, 2195,
## 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
rep_change_W

#Explore Vote Count by Swing States
c = CountyChoropleth$new(rep_votes)
c$title = "Change from Romney to Trump (Red = Better Trump) - Swing States"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(7)
c$ggplot_scale = scale_fill_manual(values = c("white","red","darkred","lightpink","blue","navy","deepskyblue"))
c$set_zoom(c("new hampshire","pennsylvania","ohio","michigan","north carolina","florida","arizona","iowa","nevada","wisconsin","virginia","colorado","minnesota","maine"))
rep_change_swing = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 51515
rep_change_swing

#Interesting State Examiniation

Examine Wisconsin

#Clinton
c = CountyChoropleth$new(dem_votes)
c$title = "Change from Obama to Clinton (Blue = Better Clinton) - Wisconsin"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(4)
c$ggplot_scale = scale_fill_manual(values = c("blue","red","darkred","lightpink"))
c$set_zoom("wisconsin")
dem_change_WI = c$render() + 
              theme(legend.position = "right")
dem_change_WI

#Trump
c = CountyChoropleth$new(rep_votes)
c$title = "Change from Romney to Trump (Red = Better Trump) - Wisconsin"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(5)
c$ggplot_scale = scale_fill_manual(values = c("red","lightpink","blue","navy","deepskyblue"))
c$set_zoom("wisconsin")
rep_change_WI = c$render() + 
              theme(legend.position = "right")
rep_change_WI

#Appear to be more significant losses for Clinton than gains for Trump

Examine Texas

#Clinton
c = CountyChoropleth$new(dem_votes)
c$title = "Change from Obama to Clinton (Blue = Better Clinton) - Texas"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("white","blue","navy","deepskyblue","red","lightpink"))
c$set_zoom("texas")
dem_change_TX = c$render() + 
              theme(legend.position = "right")
dem_change_TX

#Trump
c = CountyChoropleth$new(rep_votes)
c$title = "Change from Romney to Trump (Red = Better Trump) - Texas"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("red","darkred", "lightpink","blue","navy","deepskyblue"))
c$set_zoom("texas")
rep_change_TX = c$render() + 
              theme(legend.position = "right")
rep_change_TX

Examine Arizona

#Clinton
c = CountyChoropleth$new(dem_votes)
c$title = "Change from Obama to Clinton (Blue = Better Clinton) - Arizona"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(5)
c$ggplot_scale = scale_fill_manual(values = c("blue","navy","deepskyblue","red","lightpink"))
c$set_zoom("arizona")
dem_change_AZ = c$render() + 
              theme(legend.position = "right")
dem_change_AZ

#Trump
c = CountyChoropleth$new(rep_votes)
c$title = "Change from Romney to Trump (Red = Better Trump) - Arizona"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(5)
c$ggplot_scale = scale_fill_manual(values = c("red","lightpink","blue","navy","deepskyblue"))
c$set_zoom("arizona")
rep_change_AZ = c$render() + 
              theme(legend.position = "right")
rep_change_AZ

Percentage Shift from 2012

votes$per_shift = votes$Trump_Romney - votes$Clinton_Obama

shift = votes[,c(1,98)]

summary(shift$per_shift)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## -0.41210  0.04355  0.10060  0.10620  0.17450  0.46760
ggplot(shift,aes(x="distribution",y=per_shift)) + geom_boxplot(fill = "firebrick", colour = "darkblue") + ggtitle("County Shifts toward Republican from 2012 to 2016") + ylab("Percentage Shift toward Republican from 2012 to 2016")

shift[,3] = NA
colnames(shift) = c('region','shift','value')

for(i in seq(1:dim(shift)[1])){
  if(shift[i,2] > 0 && shift[i,2] < .05){
    shift[i,3] = "GOP - Small (<5%)"
  } else if(shift[i,2] > .05 && shift[i,2] < .10){
    shift[i,3] = "GOP - Considerable (<10%)"
  } else if(shift[i,2] >= .10){
    shift[i,3] = "GOP - Large (>10%)"
  } else if(shift[i,2] < 0 && shift[i,2] > -.05) {
    shift[i,3] = "Dem - Small (<5%)"
  } else if(shift[i,2] < -.05 && shift[i,2] > -.10){
    shift[i,3] = "Dem - Considerable (<10%)"
  } else if(shift[i,2] < -.10){
    shift[i,3] = "Dem - Large (>10%)"
  } else{
  shift[i,3] = "Equal"
  }
}

#Entire country
c = CountyChoropleth$new(shift)
c$title = "Shift from 2012 to 2016 by County Percentage"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("blue","navy","deepskyblue","red","darkred","lightpink"))
per_change_US = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 2180, 2188, 2240, 2090, 2198,
## 15005, 2100, 2170, 51515, 2016, 2060, 2290, 2282, 2070, 2110, 2130, 2185,
## 2195, 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
per_change_US

#Break down county vote into regions of the US for easier viewing

##New England Region
c = CountyChoropleth$new(shift)
c$title = "Percentage Shift from 2012 to 2016 - New England"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("blue","navy","deepskyblue","red","darkred","lightpink"))
c$set_zoom(c("maine","new hampshire", "vermont", "massachusetts","connecticut","rhode island"))
per_change_NE = c$render() + 
              theme(legend.position = "right")
per_change_NE

##Mid-Atlantic Region
c = CountyChoropleth$new(shift)
c$title = "Percentage Shift from 2012 to 2016 - Mid-Atlantic"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("blue","navy","deepskyblue","red","darkred","lightpink"))
c$set_zoom(c("new york", "pennsylvania", "new jersey", "maryland","delaware"))
per_change_MA = c$render() + 
              theme(legend.position = "right")
per_change_MA

##South East Region
c = CountyChoropleth$new(shift)
c$title = "Percentage Shift from 2012 to 2016 - South East"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("blue","navy","deepskyblue","red","darkred","lightpink"))
c$set_zoom(c("west virginia","virginia","tennessee","kentucky","north carolina","south carolina","georgia","florida","arkansas","mississippi","alabama","louisiana"))
per_change_SE = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 51515
per_change_SE

##Mid West Region
c = CountyChoropleth$new(shift)
c$title = "Percentage Shift from 2012 to 2016 - Mid West"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("blue","navy","deepskyblue","red","darkred","lightpink"))
c$set_zoom(c("ohio","michigan","indiana","illinois","wisconsin","minnesota","iowa","missouri","north dakota","south dakota","nebraska","kansas"))
per_change_MW = c$render() + 
              theme(legend.position = "right")
per_change_MW

##South West Region
c = CountyChoropleth$new(shift)
c$title = "Percentage Shift from 2012 to 2016 - South West"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("blue","navy","deepskyblue","red","darkred","lightpink"))
c$set_zoom(c("texas","oklahoma","new mexico","arizona"))
per_change_SW = c$render() + 
              theme(legend.position = "right")
per_change_SW

##West Region
c = CountyChoropleth$new(shift)
c$title = "Percentage Shift from 2012 to 2016 - West"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("blue","navy","deepskyblue","red","darkred","lightpink"))
c$set_zoom(c("colorado","wyoming","montana","idaho","utah","nevada","california","oregon","washington","alaska","hawaii"))
per_change_W = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 2180, 2188, 2240, 2090, 2198,
## 15005, 2100, 2170, 2016, 2060, 2290, 2282, 2070, 2110, 2130, 2185, 2195,
## 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
per_change_W

#Explore Vote Count by Swing States
c = CountyChoropleth$new(shift)
c$title = "Percentage Shift from 2012 to 2016 - Swing States"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("blue","navy","deepskyblue","red","darkred","lightpink"))
c$set_zoom(c("new hampshire","pennsylvania","ohio","michigan","north carolina","florida","arizona","iowa","nevada","wisconsin","virginia","colorado","minnesota","maine"))
per_change_swing = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 51515
per_change_swing

Analyze shifts at county level

total_shift = merge(x=dem_votes,y=rep_votes,by="region",all=TRUE)
total_shift[,6] = NA
colnames(total_shift) = c("region","dem_votes","dem_gain_loss","rep_votes","rep_gain_loss","value")

for(i in seq(i:dim(total_shift)[1])){
  if(total_shift[i,2] < 0){
    if(total_shift[i,4] < 0){
      total_shift[i,6] = "Dem Loss/GOP Loss"
    } else if(total_shift[i,4] > 0) {
      total_shift[i,6] = "Dem Loss/GOP Gain"
    } else {
      total_shift[i,6] = "Dem Loss/GOP Equal"
    }
  } else if(total_shift[i,2] > 0){
    if(total_shift[i,4] < 0){
      total_shift[i,6] = "Dem Gain/GOP Loss"
    } else if(total_shift[i,4] > 0){
      total_shift[i,6] = "Dem Gain/GOP Gain"
    } else {
      total_shift[i,6] = "Dem Gain/GOP Equal"
    }
  } else {
    if(total_shift[i,4] < 0){
      total_shift[i,6] = "Dem Equal/GOP Loss"
    } else if(total_shift[i,4] > 0){
      total_shift[i,6] = "Dem Equal/GOP Gain"
    } else {
      total_shift[i,6] = "Dem Equal/GOP Equal"
    }
  }
}

#Entire country
c = CountyChoropleth$new(total_shift)
c$title = "Shift from 2012 to 2016 by County Total Votes for Parties"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(7)
c$ggplot_scale = scale_fill_manual(values = c("lightpink", "deepskyblue","yellow","navy","grey","red","green"))
shift_US = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 2180, 2188, 2240, 2090, 2198,
## 15005, 2100, 2170, 51515, 2016, 2060, 2290, 2282, 2070, 2110, 2130, 2185,
## 2195, 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
shift_US

#Break down county vote into regions of the US for easier viewing

#New England Region
c = CountyChoropleth$new(total_shift)
c$title = "Shift from 2012 to 2016 by County Total Votes - New England"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(4)
c$ggplot_scale = scale_fill_manual(values = c("yellow","navy","red","green"))
c$set_zoom(c("maine","new hampshire", "vermont", "massachusetts","connecticut","rhode island"))
shift_NE = c$render() + 
              theme(legend.position = "right")
shift_NE

##Mid-Atlantic Region
c = CountyChoropleth$new(total_shift)
c$title = "Shift from 2012 to 2016 by County Total Votes - Mid-Atlantic"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(4)
c$ggplot_scale = scale_fill_manual(values = c("yellow","navy","red","green"))
c$set_zoom(c("new york", "pennsylvania", "new jersey", "maryland","delaware"))
shift_MA = c$render() + 
              theme(legend.position = "right")
shift_MA

##South East Region
c = CountyChoropleth$new(total_shift)
c$title = "Shift from 2012 to 2016 by County Total Votes - South East"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(5)
c$ggplot_scale = scale_fill_manual(values = c("lightpink","yellow","navy","red","green"))
c$set_zoom(c("west virginia","virginia","tennessee","kentucky","north carolina","south carolina","georgia","florida","arkansas","mississippi","alabama","louisiana"))
shift_SE = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 51515
shift_SE

##Mid West Region
c = CountyChoropleth$new(total_shift)
c$title = "Shift from 2012 to 2016 by County Total Votes - Mid West"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(4)
c$ggplot_scale = scale_fill_manual(values = c("yellow","navy","red","green"))
c$set_zoom(c("ohio","michigan","indiana","illinois","wisconsin","minnesota","iowa","missouri","north dakota","south dakota","nebraska","kansas"))
shift_MW = c$render() + 
              theme(legend.position = "right")
shift_MW

##South West Region
c = CountyChoropleth$new(total_shift)
c$title = "Shift from 2012 to 2016 by County Total Votes - South West"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(5)
c$ggplot_scale = scale_fill_manual(values = c("lightpink","yellow","navy","red","green"))
c$set_zoom(c("texas","oklahoma","new mexico","arizona"))
shift_SW = c$render() + 
              theme(legend.position = "right")
shift_SW

##West Region
c = CountyChoropleth$new(total_shift)
c$title = "Shift from 2012 to 2016 by County Total Votes - West"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("lightpink","yellow","navy","grey","red","green"))
c$set_zoom(c("colorado","wyoming","montana","idaho","utah","nevada","california","oregon","washington","alaska","hawaii"))
shift_W = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 2180, 2188, 2240, 2090, 2198,
## 15005, 2100, 2170, 2016, 2060, 2290, 2282, 2070, 2110, 2130, 2185, 2195,
## 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
shift_W

#Explore Vote Count by Swing States
c = CountyChoropleth$new(total_shift)
c$title = "Shift from 2012 to 2016 by County Total Votes - Swing States"
c$add_state_outline = TRUE
c$legend = "Change in Votes"
c$set_num_colors(5)
c$ggplot_scale = scale_fill_manual(values = c("yellow","navy","lightpink","red","green"))
c$set_zoom(c("new hampshire","pennsylvania","ohio","michigan","north carolina","florida","arizona","iowa","nevada","wisconsin","virginia","colorado","minnesota","maine"))
shift_swing = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 51515
shift_swing

Vote Share at State Level

#create empty vector to store the state fips values
state_levels = vector()

#store unique fips in vector
for(i in seq(i:dim(votes)[1])){
  if(!is.element(votes[i,18],state_levels)){
    state_levels = c(state_levels,votes[i,18])
  }
}

#create dataframe, add state_levels to it, rename it to "fips.numeric" in order to join with state.regions data to get the region names
states = data.frame(state_levels)
states[,c(2:9)] = 0
colnames(states)[1] = "fips.numeric"

#read in data, merge, drop duplicate column, and rename columns
data(state.regions)
states = merge(x=states,y=state.regions,by="fips.numeric")
states = states[,c(1:11)]
colnames(states) = c("fips", "dem_votes_2016", "gop_votes_2016", "total_votes_2016", "dem_votes_2012", "gop_votes_2012", "total_votes_2012","pop_2010","pop_2014","region","abb")

for(i in seq(1:dim(votes)[1])){
  for(j in seq(1:dim(states)[1])){
    if(votes[i,12] == states[j,11]){
      states[j,2] = states[j,2] + votes[i,5]
      states[j,3] = states[j,3] + votes[i,6]
      states[j,4] = states[j,4] + votes[i,7]
      states[j,5] = states[j,5] + votes[i,15]
      states[j,6] = states[j,6] + votes[i,16]
      states[j,7] = states[j,7] + votes[i,14]
      states[j,8] = states[j,8] + votes[i,27]
      states[j,9] = states[j,9] + votes[i,26]
    }
  }
}

#Plot Voter Participation Changes
states$participation_change = states$total_votes_2016 - states$total_votes_2012

voting_num = states[,c(10,12)]
colnames(voting_num) = c("region","value")

ggplot(voting_num,aes(x="distribution",y=value)) + geom_boxplot(fill = "firebrick", colour = "darkblue") + ggtitle("County Shifts in Total Votes Cast from 2012 to 2016") + ylab("Total Number of Votes Cast Change")

c = StateChoropleth$new(voting_num)
c$title = "Shift from 2012 to 2016 by Total Number of Votes Cast"
c$add_state_outline = TRUE
c$legend = "Change in Votes Cast"
c$set_num_colors(1)
vote_US = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: alaska
vote_US

states$pop_2012 = states$pop_2010 + (states$pop_2014 - states$pop_2010)

states$pop_2016 = states$pop_2014 + (states$pop_2014 - states$pop_2010)

states$per_vot_change = 100 * ((states$total_votes_2016/states$pop_2016) - (states$total_votes_2012/states$pop_2012))

per_vote_num = states[,c(10,15)]
colnames(per_vote_num)= c("region","value")


c = StateChoropleth$new(per_vote_num)
c$title = "Shift in Turnout from 2012 to 2016 by Estimated Population Size"
c$add_state_outline = TRUE
c$legend = "% Change in Votes Cast"
c$set_num_colors(8)
c$ggplot_scale = scale_fill_manual(values = c("navy","blue","lightblue","white", "lightpink", "red", "firebrick","darkred"))
per_vote_US = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: alaska
per_vote_US

#Results
states$DT_per = states$gop_votes_2016/states$total_votes_2016

states$HC_per = states$dem_votes_2016/states$total_votes_2016

states$third = (1 - states$DT_per - states$HC_per) * 100

states$DT_margin = (states$DT_per - states$HC_per) * 100

ggplot(states,aes(x="distribution",y=DT_margin)) + geom_boxplot(fill = "firebrick", colour = "darkblue") + ggtitle("% Donald Trump Won Per State") + ylab("Donald Trump %")

states$winner = NA

for(i in seq(1:dim(states)[1])){
  if(states[i,19] < -10){
    states[i,20] = "Clinton (>10%)"
  } 
  if(states[i,19] > -10 && states[i,19] < -5){
    states[i,20] = "Clinton (5% - 10%)"
  }
  if(states[i,19] > -5 && states[i,19] < -2){
    states[i,20] = "Clinton (2% - 5%)"
  } 
  if(states[i,19] > -2 && states[i,19] < 0){
    states[i,20] = "Clinton (<2%)"
  } 
  if(states[i,19] > 10){
    states[i,20] = "Trump (>10%)"
  } 
  if(states[i,19] < 10 && states[i,19] > 5){
    states[i,20] = "Trump (5% - 10%)"
  } 
  if(states[i,19] < 5 && states[i,19] > 2){
    states[i,20] = "Trump (2% - 5%)"
  } 
  if(states[i,19] < 2 && states[i,19] > 0){
    states[i,20] = "Trump (<2%)"
  } 
}

state_winner = states[,c(10,20)]
colnames(state_winner) = c("region","value")

c = StateChoropleth$new(state_winner)
c$title = "2016 State Winner Margin"
c$add_state_outline = TRUE
c$legend = "Win Margin %"
c$set_num_colors(8)
c$ggplot_scale = scale_fill_manual(values = c("lightblue","navy", "dodgerblue1","blue",  "lightpink", "firebrick","red", "darkred"))
winner_US = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: alaska
winner_US

#Third Party Vote
ggplot(states,aes(x="distribution",y=third)) + geom_boxplot(fill = "firebrick", colour = "darkblue") + ggtitle("% Third Party Per State") + ylab("Third Party Voting Percent per State")

state_third = states[,c(10,18)]

state_third$category = NA

for(i in seq(1:dim(state_third)[1])){
  if(state_third[i,2] < 3){
    state_third[i,3] = "< 3%"
  }
  if(state_third[i,2] > 3 && state_third[i,2] < 5){
    state_third[i,3] = "3% - 5%"
  }
  if(state_third[i,2] > 5 && state_third[i,2] < 7){
    state_third[i,3] = "5% - 7%"
  }
  if(state_third[i,2] > 7 && state_third[i,2] < 10){
    state_third[i,3] = "7% - 10%"
  }
  if(state_third[i,2] > 10 && state_third[i,2] < 15){
    state_third[i,3] = "10% - 15%"
  }
  if(state_third[i,2] > 15){
    state_third[i,3] = "> 15%"
  }
}

colnames(state_third) = c("region","third", "value")

c = StateChoropleth$new(state_third)
c$title = "2016 Third Party Vote by State"
c$add_state_outline = TRUE
c$legend = "Third Party %"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values = c("purple", "red","orange","blue", "green","yellow"))
state_third_US = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: alaska
state_third_US

Examine County Level Winner

county_winner = votes[,c(1,8,9)]

county_winner$Trump_margin = (county_winner$Trump - county_winner$Clinton) * 100


for(i in seq(1:dim(county_winner)[1])){
  if(county_winner[i,4] < -10){
    county_winner[i,5] = "Clinton (>10%)"
  } 
  if(county_winner[i,4] > -10 && county_winner[i,4] < -5){
    county_winner[i,5] = "Clinton (5% - 10%)"
  }
  if(county_winner[i,4] > -5 && county_winner[i,4] < -2){
    county_winner[i,5] = "Clinton (2% - 5%)"
  } 
  if(county_winner[i,4] > -2 && county_winner[i,4] < 0){
    county_winner[i,5] = "Clinton (<2%)"
  } 
  if(county_winner[i,4] > 10){
    county_winner[i,5] = "Trump (>10%)"
  } 
  if(county_winner[i,4] < 10 && county_winner[i,4] > 5){
    county_winner[i,5] = "Trump (5% - 10%)"
  } 
  if(county_winner[i,4] < 5 && county_winner[i,4] > 2){
    county_winner[i,5] = "Trump (2% - 5%)"
  } 
  if(county_winner[i,4] < 2 && county_winner[i,4] > 0){
    county_winner[i,5] = "Trump (<2%)"
  } 
}


colnames(county_winner)[1] = "region"
colnames(county_winner)[5] = "value"

plot(density(county_winner[,4]), 
         main = "Trump County Margin Density Plot",
         ylab = "density")

c = CountyChoropleth$new(county_winner)
c$title = "County Winner Margin %"
c$add_state_outline = TRUE
c$legend = "County Winner Margin"
c$set_num_colors(8)
c$ggplot_scale = scale_fill_manual(values = c("lightblue","navy", "dodgerblue1","blue",  "lightpink", "darkred","red", "firebrick"))
county_US = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 2180, 2188, 2240, 2090, 2198,
## 15005, 2100, 2170, 51515, 2016, 2060, 2290, 2282, 2070, 2110, 2130, 2185,
## 2195, 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
county_US

#Break Down By Region for Easy Viewing

#New England
c = CountyChoropleth$new(county_winner)
c$title = "County Winner Margin % - New England"
c$add_state_outline = TRUE
c$legend = "County Winner Margin"
c$set_num_colors(8)
c$ggplot_scale = scale_fill_manual(values = c("lightblue","navy", "dodgerblue1","blue",  "lightpink", "darkred","red", "firebrick"))
c$set_zoom(c("maine","new hampshire", "vermont", "massachusetts","connecticut","rhode island"))
county_NE = c$render() + 
              theme(legend.position = "right")
county_NE

##Mid-Atlantic Region
c = CountyChoropleth$new(county_winner)
c$title = "County Winner Margin % - Mid-Atlantic"
c$add_state_outline = TRUE
c$legend = "County Winner Margin"
c$set_num_colors(8)
c$ggplot_scale = scale_fill_manual(values = c("lightblue","navy", "dodgerblue1","blue",  "lightpink", "darkred","red", "firebrick"))
c$set_zoom(c("new york", "pennsylvania", "new jersey", "maryland","delaware"))
county_MA = c$render() + 
              theme(legend.position = "right")
county_MA

##South East Region
c = CountyChoropleth$new(county_winner)
c$title = "County Winner Margin % - South East"
c$add_state_outline = TRUE
c$legend = "County Winner Margin"
c$set_num_colors(8)
c$ggplot_scale = scale_fill_manual(values = c("lightblue","navy", "dodgerblue1","blue",  "lightpink", "darkred","red", "firebrick"))
c$set_zoom(c("west virginia","virginia","tennessee","kentucky","north carolina","south carolina","georgia","florida","arkansas","mississippi","alabama","louisiana"))
county_SE = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 51515
county_SE

##Mid West Region
c = CountyChoropleth$new(county_winner)
c$title = "County Winner Margin % - Mid West"
c$add_state_outline = TRUE
c$legend = "County Winner Margin"
c$set_num_colors(8)
c$ggplot_scale = scale_fill_manual(values = c("lightblue","navy", "dodgerblue1","blue",  "lightpink", "darkred","red", "firebrick"))
c$set_zoom(c("ohio","michigan","indiana","illinois","wisconsin","minnesota","iowa","missouri","north dakota","south dakota","nebraska","kansas"))
county_MW = c$render() + 
              theme(legend.position = "right")
county_MW

##South West Region
c = CountyChoropleth$new(county_winner)
c$title = "County Winner Margin % - South West"
c$add_state_outline = TRUE
c$legend = "County Winner Margin"
c$set_num_colors(8)
c$ggplot_scale = scale_fill_manual(values = c("navy", "dodgerblue1","blue",  "lightpink", "darkred","red", "firebrick"))
c$set_zoom(c("texas","oklahoma","new mexico","arizona"))
county_SW = c$render() + 
              theme(legend.position = "right")
county_SW

##West Region
c = CountyChoropleth$new(county_winner)
c$title = "County Winner Margin % - West"
c$add_state_outline = TRUE
c$legend = "County Winner Margin"
c$set_num_colors(8)
c$ggplot_scale = scale_fill_manual(values = c("lightblue","navy", "dodgerblue1","blue",  "lightpink", "darkred","red", "firebrick"))
c$set_zoom(c("colorado","wyoming","montana","idaho","utah","nevada","california","oregon","washington","alaska","hawaii"))
county_W = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 2180, 2188, 2240, 2090, 2198,
## 15005, 2100, 2170, 2016, 2060, 2290, 2282, 2070, 2110, 2130, 2185, 2195,
## 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
county_W

#Explore Vote Count by Swing States
c = CountyChoropleth$new(county_winner)
c$title = "County Winner Margin % - Swing States"
c$add_state_outline = TRUE
c$legend = "County Winner Margin"
c$set_num_colors(8)
c$ggplot_scale = scale_fill_manual(values = c("lightblue","navy", "dodgerblue1","blue",  "lightpink", "darkred","red", "firebrick"))
c$set_zoom(c("new hampshire","pennsylvania","ohio","michigan","north carolina","florida","arizona","iowa","nevada","wisconsin","virginia","colorado","minnesota","maine"))
county_swing = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 51515
county_swing

Analyze Counties that Flipped 2012 to 2016

votes$flips = NA

for(i in seq(1:dim(votes)[1])){
  if(votes[i,9] > votes[i,8] && votes[i,19] > votes[i,20]){
    votes[i,99] = "OBAMA to TRUMP"
  } else if(votes[i,9] < votes[i,8] && votes[i,19] < votes[i,20]){
    votes[i,99] = "ROMNEY to CLINTON"
  } else {
    votes[i,99] = "Solid County"
  }
}

flips = votes[,c(1,99)]
colnames(flips) = c("region","value")

c = CountyChoropleth$new(flips)
c$title = "County Flips from 2012 to 2016"
c$add_state_outline = TRUE
c$legend = "County Status"
c$set_num_colors(3)
c$ggplot_scale = scale_fill_manual(values = c("red","blue","white"))
county_flips = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 2180, 2188, 2240, 2090, 2198,
## 15005, 2100, 2170, 51515, 2016, 2060, 2290, 2282, 2070, 2110, 2130, 2185,
## 2195, 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
county_flips

#Number of Clinton Flips
length(which(votes$flips == "ROMNEY to CLINTON"))
## [1] 20
#Number of Trump Flips
length(which(votes$flips == "OBAMA to TRUMP"))
## [1] 218
#Number of counties that did not change
length(which(votes$flips == "Solid County"))
## [1] 2874

Exploratory Analysis - Religion

Explore religious variables

#total religious population
religious = votes[,c(1,83)]
colnames(religious) = c("region","value")

c= CountyChoropleth$new(religious)
c$title = "Total Religious Population"
c$add_state_outline = TRUE
c$legend = "Religious Percentage"
county_religious = c$render() + theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 48301, 2180, 2188, 2240, 2090,
## 2198, 15005, 2100, 2170, 51515, 2016, 2060, 2290, 2282, 8014, 32009, 2070,
## 2110, 2130, 2185, 2195, 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
county_religious

#Evangelical Population
evangelical = votes[,c(1,84)]
colnames(evangelical) = c("region","value")
evangelical$value = cut(evangelical$value, breaks = c(0,1,5,10,20,Inf))

c= CountyChoropleth$new(evangelical)
c$title = "Evangelical Population"
c$add_state_outline = TRUE
c$set_num_colors(5)
c$ggplot_scale = scale_fill_manual(values = c("white","darkseagreen1", "greenyellow","green",  "darkgreen"))
c$legend = "Evangelical Percentage"
county_evangelical = c$render() + theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 16081, 31007, 2050, 2105, 2122, 49009, 46075, 46117, 2150, 2164,
## 48301, 2180, 38087, 49033, 2188, 2240, 2090, 2198, 15005, 31005, 2100,
## 2170, 48269, 51515, 2016, 2060, 48109, 2290, 31165, 32029, 30103, 38085,
## 30039, 2282, 48261, 31113, 8014, 32009, 31085, 31117, 8047, 49029, 2070,
## 2110, 2130, 2185, 2195, 2220, 2230, 2020, 2068, 8023, 2013, 2261, 2270,
## 2275, 16071, 16025, 16033, 16041
county_evangelical

#Catholic Population
catholic = votes[,c(1,87)]
colnames(catholic) = c("region","value")
catholic$value = cut(catholic$value, breaks = c(0,1,5,10,20,Inf))

c= CountyChoropleth$new(catholic)
c$title = "Catholic Population"
c$add_state_outline = TRUE
c$set_num_colors(5)
c$ggplot_scale = scale_fill_manual(values = c("white","thistle1", "plum3","purple",  "purple4"))
c$legend = "Catholic Percentage"
county_catholic = c$render() + theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 16081, 28009, 28015, 28021, 28031, 13239, 47025, 47033, 47061,
## 31169, 39105, 47067, 47087, 47095, 47135, 47137, 40029, 48045, 48081,
## 38027, 38039, 18115, 40085, 40099, 40107, 30071, 48403, 48417, 48431,
## 30109, 31007, 35021, 47175, 45061, 31115, 28101, 28119, 2050, 28129, 40149,
## 29017, 48447, 49001, 29061, 29067, 29079, 37011, 2105, 42023, 17169, 41049,
## 29129, 37095, 37111, 37143, 2122, 13125, 49009, 49023, 48033, 29175, 29181,
## 29197, 29203, 13169, 45017, 46063, 46075, 13197, 13201, 2150, 13195, 13205,
## 13209, 13213, 13221, 13229, 13231, 13235, 13269, 20187, 20189, 20207,
## 21201, 13243, 22091, 13253, 13265, 13281, 13289, 13295, 13301, 2164, 48301,
## 20049, 56017, 56019, 54027, 54059, 56023, 51015, 51025, 51036, 51049,
## 2180, 51069, 51081, 51091, 51103, 53059, 54017, 54043, 21007, 21061, 21069,
## 21131, 38087, 49033, 49055, 1041, 1057, 1129, 21165, 2188, 51079, 12129,
## 13011, 13019, 13027, 21187, 13033, 13061, 2240, 5149, 28023, 1019, 2090,
## 13007, 13065, 28069, 22013, 2198, 47127, 8079, 47133, 47159, 45069, 46077,
## 51097, 1035, 1063, 1075, 12077, 29185, 30021, 40061, 40069, 21031, 5109,
## 5117, 13307, 13315, 15005, 32011, 21063, 32027, 21087, 13105, 21103, 31005,
## 49017, 48059, 21137, 48075, 2100, 2170, 1105, 1111, 17087, 17151, 48119,
## 48269, 18005, 13143, 13183, 13193, 13207, 13219, 13237, 13249, 37007,
## 37033, 37073, 51133, 51179, 51515, 13259, 13283, 20025, 20033, 37179,
## 1133, 2016, 2060, 21203, 21235, 22025, 22083, 48101, 48111, 48125, 48345,
## 48351, 51163, 51181, 51183, 48197, 48237, 51640, 51685, 51750, 53069, 8113,
## 2290, 18171, 20129, 5021, 21119, 21177, 19051, 19185, 37079, 20021, 28055,
## 28111, 28125, 28131, 47023, 40067, 40105, 40151, 28037, 40129, 28061,
## 31165, 31175, 28161, 29005, 29025, 32015, 30103, 38085, 39163, 47073,
## 47097, 47121, 48407, 48495, 29199, 31015, 31021, 24019, 29063, 37015,
## 37029, 40001, 40007, 40041, 47171, 29227, 37177, 31103, 37131, 5075, 5077,
## 2282, 16007, 26083, 18155, 13167, 13177, 13287, 54063, 51071, 51077, 51089,
## 51159, 51175, 51570, 51735, 47169, 5049, 5127, 47015, 38083, 13251, 13263,
## 13273, 21223, 21089, 51021, 51027, 51037, 29211, 8014, 30037, 31137, 31171,
## 8025, 31183, 32009, 32033, 30069, 30107, 31009, 31073, 31085, 31097, 31117,
## 28103, 38007, 12125, 13001, 45005, 45009, 47007, 47057, 13055, 47081,
## 47173, 45049, 46095, 13079, 41069, 48393, 48433, 48009, 48011, 48079,
## 48095, 8053, 48159, 8057, 48247, 48263, 48349, 51530, 8111, 54013, 49029,
## 49031, 51007, 51017, 51063, 51075, 51111, 1029, 1037, 1059, 1067, 1085,
## 2070, 2110, 2130, 2185, 2195, 2220, 2230, 5147, 6003, 1119, 1131, 2020,
## 2068, 1007, 1011, 6049, 1065, 2013, 13149, 2261, 2270, 13093, 13101, 13119,
## 8061, 12089, 13003, 13005, 13023, 13025, 13037, 13053, 2275, 12041, 1079,
## 8081, 8103, 5025, 5073, 5081, 5099, 13321, 5013, 16071, 16077, 5101, 5111,
## 17047, 5129, 16025, 16033, 16041, 13309, 16065, 13313, 13141, 13155, 13159,
## 13171, 13319, 20099, 22081, 21139, 21153, 21159, 13181, 19075, 16051,
## 20017, 13211
county_catholic

#Mormon Population
mormon = votes[,c(1,89)]
colnames(mormon) = c("region","value")
mormon$value = cut(mormon$value, breaks = c(0,1,5,10,20,Inf))

c= CountyChoropleth$new(mormon)
c$title = "Mormon Population"
c$add_state_outline = TRUE
c$legend = "Mormon Percentage"
c$set_num_colors(5)
c$ggplot_scale = scale_fill_manual(values = c("white","lightpink", "lightpink3","firebrick1",  "firebrick4"))
county_mormon = c$render() + theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 1001, 27097, 27099, 27111, 27119, 27129, 27133, 19059, 27141,
## 27149, 27157, 27163, 28005, 17005, 28009, 28011, 28015, 28021, 19149,
## 28045, 28051, 28053, 17009, 28067, 28073, 13239, 27077, 45031, 45035,
## 45037, 47017, 47021, 47025, 47033, 47041, 47047, 23015, 28041, 31149,
## 31151, 17035, 31169, 31181, 32005, 32013, 39105, 39107, 39117, 39125,
## 39129, 39139, 39149, 39161, 47067, 47071, 47077, 47083, 47087, 17065,
## 47095, 47101, 47107, 47109, 47111, 47119, 47135, 47137, 1107, 18051, 18059,
## 33019, 39167, 39175, 40003, 40011, 40025, 40029, 40035, 40045, 40055,
## 48045, 48055, 48065, 48073, 48077, 48081, 48087, 24047, 25007, 25015,
## 34033, 34041, 30055, 37195, 37199, 38005, 38009, 38027, 38029, 38037,
## 38039, 38043, 38047, 38053, 18115, 38059, 38063, 40085, 40091, 40099,
## 18125, 40101, 40107, 26003, 18131, 30091, 30097, 1121, 18139, 38065, 38067,
## 38071, 38075, 38081, 38089, 38093, 38097, 39001, 18153, 39011, 39013,
## 39019, 48369, 18173, 48385, 48387, 48399, 48403, 48417, 48419, 48427,
## 18179, 48431, 26019, 26027, 30109, 31007, 31011, 31013, 31019, 31025,
## 31029, 31039, 31051, 31057, 31071, 19009, 39033, 35019, 35021, 35025,
## 35033, 47143, 47147, 47155, 47161, 47167, 19021, 47175, 45055, 45061,
## 45065, 45071, 45073, 26041, 19031, 17131, 13191, 31079, 31083, 31087,
## 31099, 19039, 31101, 31105, 31115, 31119, 28097, 28101, 28119, 2050, 28129,
## 36013, 40137, 40145, 17079, 40149, 41013, 41021, 26087, 26093, 26095,
## 28141, 28143, 17103, 28155, 28157, 28163, 29017, 29023, 29035, 29041,
## 29045, 17107, 36041, 36051, 36073, 48435, 48437, 48447, 48483, 17129,
## 48489, 26071, 26131, 26135, 17139, 29051, 29055, 29057, 29067, 29073,
## 17145, 29087, 2105, 17155, 36093, 36099, 42023, 17169, 45089, 46033, 46039,
## 46045, 46051, 17175, 46059, 26159, 27011, 27017, 27025, 27041, 27043,
## 27059, 37037, 17189, 37059, 37069, 37075, 37077, 17203, 42029, 42049,
## 42067, 42073, 27063, 27065, 27069, 29111, 29125, 29129, 29139, 29141,
## 29153, 29157, 37095, 37115, 18033, 37121, 37139, 37143, 37153, 2122, 13125,
## 42109, 42119, 13129, 48017, 48023, 48033, 13131, 48035, 29171, 29186,
## 29197, 29207, 37169, 39071, 39077, 44001, 44003, 45001, 45017, 45023,
## 46063, 46065, 46069, 46075, 46079, 46085, 46097, 46101, 46115, 46117,
## 46127, 30005, 30019, 31129, 13197, 31133, 19015, 19019, 19023, 19027,
## 13201, 17091, 17093, 17099, 17101, 17117, 17125, 17127, 2150, 13189, 13195,
## 13199, 13205, 13209, 13213, 13221, 13229, 13231, 13225, 13235, 13241,
## 13247, 13257, 13267, 13269, 20183, 20187, 20197, 20207, 21191, 21201,
## 21205, 21207, 21221, 13243, 21233, 21237, 21239, 22005, 22091, 22093,
## 22099, 22113, 13253, 21013, 21015, 21027, 17137, 13265, 17147, 17153,
## 17191, 13279, 13281, 13289, 13301, 20041, 20047, 13285, 20053, 20063,
## 20073, 20077, 20087, 22023, 22031, 22035, 22041, 22043, 13303, 21039,
## 21041, 21057, 21065, 21079, 21085, 2164, 20027, 21091, 21097, 48267, 48271,
## 48281, 48285, 48287, 48291, 48293, 48295, 48301, 48305, 48317, 48327,
## 48331, 20039, 48333, 51810, 51840, 53003, 53017, 20049, 53023, 51600,
## 55107, 55111, 55119, 55125, 20065, 56017, 56019, 54025, 54033, 20075,
## 54045, 54051, 54053, 54059, 54065, 54069, 54075, 20085, 54087, 54091,
## 54093, 54099, 54103, 54105, 55011, 55013, 20095, 55019, 55021, 55023,
## 50003, 20105, 50011, 51005, 51011, 51019, 20115, 51031, 51033, 51036,
## 51045, 51049, 51051, 54081, 2180, 20127, 51069, 51081, 51091, 51095, 51103,
## 51119, 50025, 20135, 54017, 54029, 54043, 54055, 54073, 20145, 54095,
## 51115, 22111, 21001, 21005, 21007, 21019, 21037, 21049, 21061, 21069,
## 21081, 21109, 21125, 21131, 40037, 20163, 37187, 38003, 38013, 38025,
## 38041, 38055, 38069, 38087, 38095, 39021, 54109, 55027, 50013, 20185,
## 51009, 51023, 51035, 51053, 51065, 1027, 20195, 1115, 1129, 21165, 21181,
## 19043, 20203, 19073, 19081, 19091, 19115, 19131, 19147, 19157, 20205,
## 19165, 19187, 19197, 35059, 2188, 36077, 51079, 12129, 13011, 21187, 13033,
## 13039, 13061, 2240, 21189, 6091, 8011, 8019, 20023, 27071, 27079, 27087,
## 27101, 27107, 27125, 27131, 27143, 27159, 27169, 28007, 28035, 28043,
## 36113, 42025, 42053, 21219, 42075, 42099, 1013, 1019, 21229, 2090, 8049,
## 12121, 12133, 22001, 13007, 13035, 13047, 13065, 13075, 28057, 28069,
## 22119, 22125, 22013, 25003, 45033, 47051, 47075, 2198, 47099, 47127, 13085,
## 13099, 13117, 8055, 8065, 8079, 8089, 12007, 22077, 26059, 26113, 26119,
## 27013, 27023, 27033, 47133, 47159, 47165, 22105, 45059, 45069, 45087,
## 46025, 46037, 46049, 46057, 46067, 46077, 51097, 51109, 1035, 1039, 1053,
## 1063, 21003, 1075, 12047, 12067, 12077, 5011, 5037, 5055, 5065, 5079,
## 29115, 29121, 29163, 29173, 29185, 29223, 46091, 21025, 46107, 46119,
## 46129, 40057, 40061, 40069, 40075, 40093, 40103, 5141, 21031, 40133, 40141,
## 5117, 5135, 13307, 13311, 13315, 15005, 21053, 17017, 31123, 31127, 31143,
## 31167, 31179, 31185, 32011, 21063, 32027, 33017, 21077, 48363, 48373,
## 48405, 48413, 48421, 48455, 13083, 21087, 13111, 8071, 5095, 17025, 17039,
## 21099, 17061, 17069, 18049, 18075, 18093, 18113, 21103, 18119, 30049,
## 30075, 31001, 31005, 31017, 31027, 31041, 31059, 31069, 31077, 48493,
## 21121, 47183, 48019, 48059, 5145, 21137, 48075, 48089, 2100, 2170, 1105,
## 1111, 21143, 19003, 19025, 19035, 17071, 17075, 17087, 17123, 17151, 17199,
## 31107, 28077, 28081, 21157, 28107, 28123, 28133, 28145, 28159, 29003,
## 29039, 29059, 48107, 48119, 48131, 48143, 48153, 48163, 21175, 48173,
## 48175, 48219, 48255, 48269, 48279, 48289, 8033, 19047, 18027, 13127, 13133,
## 13163, 13183, 13193, 13207, 13219, 13227, 13237, 13249, 29083, 29093,
## 19063, 37003, 37007, 37009, 37073, 37085, 19067, 37123, 48299, 48335,
## 48343, 51127, 19079, 51515, 51620, 51680, 51720, 51740, 13259, 13271,
## 13283, 20025, 20031, 20033, 19095, 20069, 20081, 20093, 20101, 20111,
## 20123, 20133, 20139, 20151, 19105, 20159, 37157, 37173, 39069, 39115,
## 39127, 39137, 39165, 40005, 19121, 55053, 55067, 55078, 55099, 55121,
## 55135, 2016, 2060, 20179, 21197, 19137, 21203, 21215, 22007, 22021, 22025,
## 22047, 22059, 22067, 22083, 48101, 48105, 48109, 48111, 48125, 48133,
## 19151, 48145, 48151, 48193, 48345, 48351, 19161, 51137, 51139, 51169,
## 51171, 51181, 55035, 55037, 55041, 55043, 55047, 55049, 55057, 53043,
## 48197, 19181, 48211, 48235, 48237, 48239, 48243, 19195, 48253, 48259,
## 51191, 51195, 51520, 51610, 20005, 51640, 51678, 51683, 51685, 51710,
## 51750, 51790, 55075, 20015, 55077, 55083, 55091, 55093, 55097, 53069,
## 27073, 54005, 54009, 54011, 54015, 54019, 8093, 8107, 8113, 8115, 5125,
## 2290, 18171, 27085, 19005, 17059, 27093, 20129, 20137, 20153, 20175, 22065,
## 5021, 21119, 21127, 21133, 21149, 21155, 19051, 19065, 27113, 19071, 19083,
## 19097, 19109, 18111, 19119, 19133, 19143, 19167, 27121, 19173, 19185,
## 19191, 20001, 20007, 22071, 27075, 27123, 27127, 27151, 27155, 27165,
## 27167, 27173, 28019, 28027, 27147, 19117, 37053, 37079, 20021, 28055,
## 28063, 27153, 22087, 38021, 38023, 38033, 38049, 28105, 27161, 28111,
## 28113, 28125, 35051, 42043, 28003, 45029, 6043, 28013, 47023, 38051, 40067,
## 40077, 40089, 40095, 40105, 28017, 31139, 19037, 17083, 17085, 28037,
## 47055, 23025, 40129, 26001, 41025, 26089, 26105, 28147, 28061, 47029,
## 31165, 31175, 26011, 28065, 30079, 26109, 28149, 28161, 29005, 29025,
## 39095, 45011, 22121, 32029, 39111, 39123, 30105, 38085, 38099, 29031,
## 41055, 23009, 41063, 29113, 39163, 23029, 47089, 47091, 47097, 47115,
## 47121, 47123, 47131, 48377, 48391, 48407, 36079, 48443, 48461, 48473,
## 48479, 48495, 48501, 48007, 29199, 26013, 31015, 31021, 26153, 46071,
## 46081, 46089, 46105, 46109, 34001, 24029, 31043, 31049, 31061, 29069,
## 29081, 29089, 37015, 37023, 37029, 29133, 29143, 29149, 29155, 37113,
## 24039, 40001, 40007, 40021, 40023, 40033, 37189, 48051, 24041, 47153,
## 47163, 47171, 47181, 36097, 36105, 36115, 46009, 46021, 29209, 26009,
## 29227, 37177, 39067, 39073, 48071, 26015, 45077, 26053, 46047, 26023,
## 46053, 27007, 27015, 27021, 27027, 46135, 30025, 30033, 30045, 25005,
## 25019, 26037, 31091, 31095, 31103, 28093, 27031, 27049, 27055, 37131,
## 37137, 37149, 42103, 34025, 26057, 35005, 38001, 5041, 5057, 5075, 5077,
## 17013, 17021, 17027, 18023, 2282, 26083, 5105, 5107, 18123, 18147, 18155,
## 18163, 26085, 18039, 13145, 13167, 13177, 13179, 17161, 17171, 17173,
## 17181, 17193, 26097, 13287, 13291, 21105, 48311, 48315, 48337, 54071,
## 51077, 51083, 5108
county_mormon

#Jewish Population
jewish = votes[,c(1,88)]
colnames(jewish) = c("region","value")
jewish$value = cut(jewish$value, breaks = c(0,1,2,5,10,Inf))

c= CountyChoropleth$new(jewish)
c$title = "Jewish Population"
c$add_state_outline = TRUE
c$legend = "Jewish Percentage"
c$set_num_colors(5)
c$ggplot_scale = scale_fill_manual(values = c("white","cyan", "cyan3","blue",  "darkblue"))
county_jewish = c$render() + theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 1001, 1009, 1099, 16067, 27091, 27095, 27097, 27099, 27105,
## 27111, 27115, 27117, 27119, 16081, 27129, 27133, 19059, 27141, 27145,
## 27149, 27157, 27163, 27171, 28005, 17005, 28009, 28015, 28021, 28031,
## 28039, 19149, 28045, 28051, 28053, 17009, 28059, 28067, 28073, 23007,
## 13239, 27077, 45031, 45035, 45037, 47011, 47017, 47021, 47025, 47033,
## 47035, 47041, 47047, 47049, 47059, 17029, 47061, 23015, 23021, 23023,
## 23031, 28041, 31149, 31151, 31155, 17035, 31161, 31169, 31173, 31181,
## 32005, 32007, 32013, 32017, 32023, 17049, 39105, 39107, 39117, 39125,
## 39129, 39133, 39135, 39141, 17055, 39149, 39157, 39161, 47067, 47071,
## 47077, 47083, 47087, 17065, 47095, 47101, 47107, 47109, 47111, 47119,
## 47125, 47135, 47137, 24011, 1107, 18051, 24021, 24023, 32510, 33003, 33009,
## 18059, 33019, 39167, 39171, 39175, 40003, 40011, 18069, 40013, 40017,
## 40025, 40029, 40035, 40039, 40045, 40049, 40055, 37193, 18079, 47141,
## 48045, 48049, 48055, 48065, 48067, 48073, 48077, 48081, 18085, 48087,
## 48091, 18095, 34033, 35007, 30055, 30057, 37195, 37199, 38005, 38009,
## 38027, 38029, 38037, 38039, 38043, 38047, 38053, 18115, 38059, 38063,
## 47001, 47003, 40065, 40083, 40085, 40091, 40099, 18125, 40107, 40111,
## 40117, 40119, 40125, 26003, 26005, 18131, 26007, 30063, 30065, 30071,
## 30077, 30083, 30087, 30091, 30097, 30101, 1121, 18139, 38065, 38067, 38071,
## 38075, 38081, 38089, 38093, 38097, 38101, 39001, 18153, 39005, 39011,
## 39013, 39019, 48361, 48369, 48383, 18173, 48385, 48387, 48395, 48399,
## 48403, 48415, 48417, 48419, 48427, 18179, 48431, 26019, 26027, 26031,
## 26033, 30109, 31007, 31011, 18183, 31013, 31019, 31025, 31029, 31033,
## 31039, 31045, 31051, 31057, 31071, 19009, 31075, 39025, 39033, 39037,
## 39041, 35019, 19017, 35021, 35025, 35033, 35039, 47143, 47147, 47149,
## 47155, 47161, 47167, 19021, 47175, 47177, 45055, 45061, 45065, 45071,
## 45073, 26041, 26043, 19031, 26051, 26069, 17131, 13191, 31079, 31081,
## 31083, 31087, 31099, 19039, 31101, 31105, 31115, 31119, 28091, 28097,
## 28101, 28109, 28119, 2050, 17073, 28121, 28129, 35041, 35057, 35061, 17077,
## 36033, 40131, 40135, 40137, 40145, 17079, 40149, 41003, 41005, 41011,
## 41013, 41021, 41027, 26079, 26087, 26093, 26095, 26099, 26101, 28137,
## 28141, 28143, 28153, 17103, 28155, 28157, 28163, 29007, 29017, 29023,
## 29029, 29035, 29041, 29045, 17107, 36041, 36045, 36051, 36073, 36075,
## 48435, 17121, 48437, 48447, 48451, 48457, 48467, 48477, 48483, 17129,
## 48485, 48489, 48499, 49001, 49007, 26071, 26131, 26135, 26145, 17139,
## 26147, 26157, 29047, 29055, 29057, 29061, 29067, 29073, 17145, 29079,
## 29087, 37011, 37013, 37019, 37025, 13223, 36089, 2105, 17155, 36099, 36107,
## 36117, 42023, 17169, 45089, 46005, 46011, 46019, 46023, 46033, 46039,
## 46041, 46045, 46051, 17175, 46059, 26159, 27001, 27003, 27011, 27017,
## 27025, 27035, 17179, 27041, 27043, 27045, 27059, 37035, 37037, 37039,
## 37041, 17189, 37059, 37061, 37065, 37069, 37075, 37077, 37083, 37087,
## 17203, 42061, 42067, 18009, 41033, 41049, 41059, 41065, 41071, 18011,
## 27063, 27065, 27069, 29105, 29111, 29119, 29125, 29129, 29139, 29141,
## 18021, 29147, 29153, 29157, 29161, 29167, 37095, 37099, 37105, 37111,
## 37115, 18033, 37121, 37135, 37139, 37143, 37153, 2122, 13125, 42109, 42111,
## 42115, 42119, 49009, 49013, 13129, 49019, 49023, 48001, 48013, 48015,
## 48017, 48023, 48025, 48033, 13131, 48035, 48039, 29171, 29175, 29177,
## 29181, 29186, 29187, 29197, 29203, 13139, 29207, 29213, 29225, 30003,
## 37159, 37163, 37169, 37175, 13157, 39071, 39077, 39087, 13169, 45001,
## 45007, 45017, 45023, 45025, 46063, 46065, 46069, 13175, 46075, 46079,
## 46085, 46093, 46097, 46101, 46113, 46115, 46117, 46121, 13187, 46127,
## 30005, 30009, 30015, 30019, 30023, 30029, 30035, 30041, 31129, 13197,
## 31133, 31141, 19007, 19011, 19015, 19019, 19023, 19027, 19033, 17081,
## 13201, 17091, 17093, 17101, 17105, 17109, 17117, 17125, 17127, 2150, 13217,
## 13185, 13189, 13195, 13199, 13205, 13209, 13213, 13221, 13229, 13231,
## 13225, 13235, 13241, 13247, 13255, 13257, 13267, 13269, 20183, 20187,
## 20189, 13233, 20197, 20207, 21191, 21193, 21201, 21205, 21207, 21217,
## 21221, 21231, 13243, 21233, 21237, 21239, 22005, 22091, 22093, 22097,
## 22099, 22113, 13253, 22117, 21011, 21013, 21015, 21027, 21029, 17133,
## 17137, 17141, 13265, 17147, 17153, 17159, 17165, 17177, 17191, 13277,
## 18001, 13275, 13279, 13281, 13289, 13295, 13301, 20035, 20041, 20047,
## 13285, 20051, 20053, 20057, 20063, 20073, 20077, 20079, 20087, 22011,
## 22015, 13293, 22023, 22031, 22035, 22041, 22043, 22049, 21035, 13303,
## 21039, 21041, 21045, 21057, 21065, 21071, 21079, 21083, 21085, 2164, 20027,
## 21091, 21097, 21101, 21107, 48267, 48271, 48277, 48281, 48285, 48287,
## 20029, 48291, 48293, 48295, 48301, 48305, 48307, 48317, 48323, 48327,
## 48331, 20039, 48333, 51800, 53001, 53003, 53015, 53017, 53019, 20049,
## 53023, 53025, 51600, 55107, 55111, 55113, 55119, 55123, 55125, 20065,
## 56003, 56009, 56017, 56019, 54023, 54025, 54027, 54033, 20075, 54037,
## 54045, 54051, 54059, 54065, 54075, 48341, 54083, 20085, 54087, 54091,
## 54093, 54097, 54099, 54103, 54105, 55011, 55013, 20095, 55019, 55021,
## 55023, 53029, 49039, 49045, 49049, 20105, 50011, 50015, 56023, 51005,
## 51011, 51013, 51015, 51019, 20115, 51025, 51031, 51033, 51036, 51045,
## 51049, 51051, 51061, 51067, 2180, 20127, 51069, 51073, 51081, 51085, 51091,
## 51095, 51103, 49037, 51119, 20135, 53037, 53039, 53059, 53075, 54029,
## 54043, 54073, 54085, 20145, 54095, 51057, 51115, 22095, 22111, 21001,
## 21005, 21007, 21019, 21037, 20155, 21049, 21061, 21069, 21081, 21093,
## 21109, 21125, 21131, 40037, 40047, 20163, 37187, 38003, 38013, 38025,
## 38041, 38055, 38069, 38087, 38095, 39021, 54109, 55015, 55027, 49027,
## 49033, 49041, 49055, 50013, 20185, 51003, 51009, 51023, 51035, 51065,
## 1003, 1027, 1041, 1057, 20195, 1091, 1103, 1115, 1129, 21147, 21165, 21181,
## 19041, 19043, 20203, 19049, 19073, 19081, 19091, 19099, 19115, 19131,
## 19147, 19157, 20205, 19165, 19187, 19197, 39031, 39045, 35017, 35037,
## 35059, 2188, 21183, 36017, 36031, 36049, 51079, 12129, 13011, 13019, 13027,
## 21187, 13033, 13039, 13061, 2240, 5149, 6011, 6027, 6063, 21189, 6091,
## 8011, 8019, 8027, 20013, 20023, 27071, 27079, 21199, 27087, 27101, 27107,
## 27125, 27131, 27143, 27159, 27169, 28007, 28023, 21209, 28043, 36101,
## 42025, 42037, 42053, 21219, 42057, 42099, 42117, 42131, 1005, 1013, 1019,
## 21229, 6061, 2090, 8041, 8049, 12093, 12121, 12133, 22001, 13007, 13035,
## 13047, 13065, 13075, 28057, 28069, 22119, 22125, 22013, 23017, 24015,
## 26039, 45021, 45033, 47009, 47019, 47051, 47063, 47075, 47085, 1017, 2198,
## 22053, 47099, 47127, 6035, 13085, 13099, 13117, 8051, 8055, 8065, 8079,
## 8089, 8117, 12007, 12019, 22077, 12027, 26059, 26063, 26113, 26119, 26137,
## 26149, 27005, 22089, 27013, 27023, 27033, 27047, 27061, 47133, 47145,
## 47159, 47165, 22105, 45047, 45059, 45069, 45087, 46015, 46025, 46037,
## 46049, 46057, 22115, 46067, 46077, 51097, 51109, 1035, 1039, 1049, 1053,
## 1063, 21003, 1075, 1077, 12047, 12067, 12077, 4001, 4007, 5011, 5023,
## 21009, 5037, 5039, 5055, 5065, 5079, 29099, 29115, 29121, 29135, 29145,
## 21017, 29163, 29173, 29185, 29201, 29215, 29223, 30007, 30021, 46091,
## 21025, 46107, 46119, 46129, 40057, 40061, 40069, 40075, 40093, 40103,
## 40115, 5141, 21031, 40133, 40141, 41009, 5109, 5117, 5135, 21043, 13307,
## 13311, 13315, 15005, 16005, 16027, 16039, 16063, 16075, 21053, 17017,
## 30047, 31123, 31127, 31143, 31153, 31167, 31179, 31185, 32011, 21063,
## 32027, 41023, 41061, 42009, 21077, 48363, 48373, 48389, 48405, 48413,
## 48421, 48455, 48471, 13083, 21087, 13097, 13105, 13111, 8071, 5095, 17025,
## 17039, 21099, 17045, 17061, 17069, 18049, 18063, 18075, 18093, 18099,
## 18113, 21103, 18119, 18133, 18145, 35011, 30049, 30053, 30075, 30085,
## 31001, 21115, 31005, 31017, 31027, 31041, 31059, 31069, 31077, 31089,
## 48481, 48493, 21121, 48503, 49005, 49017, 47183, 47187, 48003, 48019,
## 48027, 48041, 48059, 5145, 21137, 48075, 48083, 48089, 2100, 2170, 1105,
## 1111, 1113, 1123, 21143, 18165, 19003, 19025, 19035, 17071, 17075, 17087,
## 21151, 17123, 17135, 17151, 17163, 17187, 17199, 31107, 28077, 28085,
## 21157, 28107, 28123, 28133, 28145, 28159, 29003, 29015, 29027, 29039,
## 21169, 29049, 29059, 29071, 48107, 48119, 48131, 48143, 48153, 48163,
## 48171, 21175, 48173, 48175, 48187, 48205, 48209, 48219,
county_jewish

#Total Christian Population
votes$Christian = votes$Evangelical + votes$Protestant + votes$Catholic + votes$Historically_Black + votes$Orthodox
christian = votes[,c(1,100)]
colnames(christian) = c("region","value")
christian$value = cut(christian$value, breaks = c(0,10,20,30,40,50,60,70,Inf))


c= CountyChoropleth$new(christian)
c$title = "Christian Population"
c$add_state_outline = TRUE
c$set_num_colors(8)
c$ggplot_scale = scale_fill_manual(values = c("white","yellow", "salmon","springgreen","brown1", "deepskyblue", "darkmagenta", "darkblue"))
c$legend = "Christian Percentage"
county_christian = c$render() + theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 16081, 31007, 2050, 2105, 2122, 49009, 46075, 2150, 2164, 48301,
## 2180, 49033, 2188, 2240, 2090, 2198, 15005, 2100, 2170, 48269, 51515, 2016,
## 2060, 2290, 2282, 8014, 32009, 31117, 49029, 2070, 2110, 2130, 2185, 2195,
## 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275, 16025
county_christian

Model Construction

Prepare Data for Modeling

#change decimals to match the other percentage values
votes$Trump = votes$Trump * 100
votes$Clinton = votes$Clinton * 100
votes$Obama = votes$Obama * 100
votes$Romney = votes$Romney * 100
votes$White = votes$White * 100
votes$Black = votes$Black * 100
votes$Hispanic = votes$Hispanic * 100
votes$Clinton_Obama = votes$Clinton_Obama * 100
votes$Trump_Romney = votes$Trump_Romney * 100
votes$per_shift = votes$per_shift * 100

Predict Clinton-Obama Deviation

CO_Dev_Predict = votes[,c(14:16,19,20,26:28,30:35,40,42:63,67:77,83:95,100)]

CO_Dev_Predict = na.omit(CO_Dev_Predict)

null_CO = lm(Clinton_Obama~1,data = CO_Dev_Predict)
full_CO = lm(Clinton_Obama~.,data = CO_Dev_Predict)

CO_Dev = step(null_CO,scope=list(upper=full_CO),data=CO_Dev_Predict,direction="both")

votes$CO_Dev_Pred = predict(CO_Dev,votes)

Predict Trump-Romney Deviation

TR_Dev_Predict =  votes[,c(14:16,19,20,26:28,30:35,40,42:63,67:76,78,83:95,100)]

TR_Dev_Predict = na.omit(TR_Dev_Predict)

null_TR = lm(Trump_Romney~1, data = TR_Dev_Predict)
full_TR = lm(Trump_Romney~., data = TR_Dev_Predict)

TR_Dev = step(null_TR,scope=list(upper=full_TR),data=TR_Dev_Predict,direction="both")

votes$TR_Dev_Pred = predict(TR_Dev, votes)

Predict Overall Deviation

Overall_Dev_Predict = votes[,c(14:16,19,20,26:28,30:35,40,42:63,67:76,83:95,98,100)]

Overall_Dev_Predict = na.omit(Overall_Dev_Predict)

null_Overall = lm(per_shift~1, data = Overall_Dev_Predict)
full_Overall = lm(per_shift~., data = Overall_Dev_Predict)

Overall_Dev = step(null_Overall,scope=list(upper=full_Overall),data=Overall_Dev_Predict, direction = "both")

votes$Overall_Dev_Pred = predict(Overall_Dev, votes)

Deviation Graphics: Clinton-Obama

summary(CO_Dev)
## 
## Call:
## lm(formula = Clinton_Obama ~ `Foreign Born` + Black + Obama + 
##     Edu_batchelors + NonEnglish + Protestant + `% Female 2014` + 
##     `Median Value of Owner-Occupied Housing Units` + Income + 
##     Hispanic + White + votes_gop_2012 + `Median Household Income` + 
##     `Manufacturers Shipments - 2007` + `Merchant Wholesaler Sales - 2007` + 
##     `Persons/Household` + `Persons Under 18` + Mormon + Edu_highschool + 
##     `Hispanic-Owned Firms` + `Travel Time to Work` + population_change + 
##     Catholic + Density + total_votes_2012 + votes_dem_2012 + 
##     population2010 + Households + `Private Nonfarm Establishments 2013` + 
##     `Living in Same House 1+ Years` + `Homeownership Rate` + 
##     `Building Permits` + `Private Nonfarm Employment` + `Total Number of Firms` + 
##     `Accommodation and Food Service Sales - 2007`, data = CO_Dev_Predict)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -9.197 -1.395  0.033  1.332 13.576 
## 
## Coefficients:
##                                                  Estimate Std. Error
## (Intercept)                                    -1.205e+01  1.893e+00
## `Foreign Born`                                  3.307e-02  1.745e-02
## Black                                           1.706e-01  6.937e-03
## Obama                                          -2.241e-01  4.136e-03
## Edu_batchelors                                  3.360e-01  1.067e-02
## NonEnglish                                      5.391e-02  1.397e-02
## Protestant                                     -2.703e-02  5.351e-03
## `% Female 2014`                                 2.108e-01  2.287e-02
## `Median Value of Owner-Occupied Housing Units`  1.377e-05  1.148e-06
## Income                                         -7.876e-05  2.546e-05
## Hispanic                                        7.682e-02  9.086e-03
## White                                          -4.670e-02  6.696e-03
## votes_gop_2012                                 -3.426e-04  6.895e-05
## `Median Household Income`                      -2.228e-05  1.158e-05
## `Manufacturers Shipments - 2007`                6.281e-08  1.790e-08
## `Merchant Wholesaler Sales - 2007`             -3.791e-08  1.737e-08
## `Persons/Household`                             1.661e+00  3.438e-01
## `Persons Under 18`                             -1.304e-01  2.172e-02
## Mormon                                          4.633e-02  1.104e-02
## Edu_highschool                                 -3.837e-02  1.201e-02
## `Hispanic-Owned Firms`                          3.846e-02  9.685e-03
## `Travel Time to Work`                          -2.961e-02  1.113e-02
## population_change                               2.790e-02  1.257e-02
## Catholic                                       -1.157e-02  5.809e-03
## Density                                        -1.018e-04  3.865e-05
## total_votes_2012                                3.509e-04  6.914e-05
## votes_dem_2012                                 -3.549e-04  6.928e-05
## population2010                                 -9.648e-06  2.803e-06
## Households                                      2.445e-05  9.459e-06
## `Private Nonfarm Establishments 2013`          -2.103e-04  8.001e-05
## `Living in Same House 1+ Years`                 4.256e-02  1.321e-02
## `Homeownership Rate`                           -2.841e-02  9.159e-03
## `Building Permits`                             -1.063e-04  6.592e-05
## `Private Nonfarm Employment`                    6.774e-06  2.759e-06
## `Total Number of Firms`                         2.486e-05  1.686e-05
## `Accommodation and Food Service Sales - 2007`  -1.427e-07  9.784e-08
##                                                t value Pr(>|t|)    
## (Intercept)                                     -6.366 2.24e-10 ***
## `Foreign Born`                                   1.895 0.058199 .  
## Black                                           24.588  < 2e-16 ***
## Obama                                          -54.186  < 2e-16 ***
## Edu_batchelors                                  31.483  < 2e-16 ***
## NonEnglish                                       3.860 0.000116 ***
## Protestant                                      -5.052 4.64e-07 ***
## `% Female 2014`                                  9.217  < 2e-16 ***
## `Median Value of Owner-Occupied Housing Units`  11.992  < 2e-16 ***
## Income                                          -3.094 0.001992 ** 
## Hispanic                                         8.455  < 2e-16 ***
## White                                           -6.975 3.74e-12 ***
## votes_gop_2012                                  -4.969 7.10e-07 ***
## `Median Household Income`                       -1.924 0.054468 .  
## `Manufacturers Shipments - 2007`                 3.509 0.000456 ***
## `Merchant Wholesaler Sales - 2007`              -2.182 0.029171 *  
## `Persons/Household`                              4.833 1.41e-06 ***
## `Persons Under 18`                              -6.005 2.13e-09 ***
## Mormon                                           4.195 2.80e-05 ***
## Edu_highschool                                  -3.195 0.001413 ** 
## `Hispanic-Owned Firms`                           3.971 7.31e-05 ***
## `Travel Time to Work`                           -2.661 0.007830 ** 
## population_change                                2.220 0.026488 *  
## Catholic                                        -1.992 0.046437 *  
## Density                                         -2.635 0.008460 ** 
## total_votes_2012                                 5.076 4.09e-07 ***
## votes_dem_2012                                  -5.122 3.21e-07 ***
## population2010                                  -3.442 0.000585 ***
## Households                                       2.584 0.009799 ** 
## `Private Nonfarm Establishments 2013`           -2.629 0.008615 ** 
## `Living in Same House 1+ Years`                  3.221 0.001289 ** 
## `Homeownership Rate`                            -3.102 0.001941 ** 
## `Building Permits`                              -1.613 0.106916    
## `Private Nonfarm Employment`                     2.456 0.014121 *  
## `Total Number of Firms`                          1.474 0.140459    
## `Accommodation and Food Service Sales - 2007`   -1.458 0.144885    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.206 on 3073 degrees of freedom
## Multiple R-squared:  0.804,  Adjusted R-squared:  0.8018 
## F-statistic: 360.3 on 35 and 3073 DF,  p-value: < 2.2e-16
votes$model_error_CO = (votes$Clinton_Obama - votes$CO_Dev_Pred)

ME_CO = votes[,c(1,104)]
colnames(ME_CO) = c("region","value")
ME_CO$value = cut(ME_CO$value, breaks = c(-10,-5,-1,1,5,10,Inf))

c= CountyChoropleth$new(ME_CO)
c$title = "Model Deviation: Clinton-Obama"
c$add_state_outline = TRUE
c$legend = "Model Deviation"
c$set_num_colors(6)
c$ggplot_scale = scale_fill_manual(values=c("red","indianred1","white","lightcyan1","dodgerblue","darkblue"))
county_ME_CO = c$render() + theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 48301, 2180, 2188, 2240, 2090,
## 2198, 15005, 2100, 2170, 51515, 2016, 2060, 2290, 2282, 8014, 32009, 2070,
## 2110, 2130, 2185, 2195, 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
county_ME_CO

Deviation Graphics: Trump-Romney

summary(TR_Dev)
## 
## Call:
## lm(formula = Trump_Romney ~ Edu_batchelors + `Persons/Household` + 
##     Mormon + Romney + Black + Hispanic + Christian + White + 
##     Income + `Median Value of Owner-Occupied Housing Units` + 
##     `Travel Time to Work` + `% Female 2014` + votes_gop_2012 + 
##     `Nonemployer Establishments - 2013` + Edu_highschool + `Land Area (in sq miles)` + 
##     `Manufacturers Shipments - 2007` + `Persons Under 5` + Other_Religion + 
##     `Merchant Wholesaler Sales - 2007` + Orthodox + `Median Household Income` + 
##     Poverty + `Hispanic-Owned Firms` + `Private Nonfarm Employment` + 
##     Density + Veterans + Jewish + `Housing Units in Multi-Unit Structures` + 
##     Obama + `Accommodation and Food Service Sales - 2007` + `Black-Owned Firms` + 
##     `Homeownership Rate` + `% Change - Private Nonfarm Employment`, 
##     data = TR_Dev_Predict)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -21.3646  -1.4701   0.0437   1.6070  18.0019 
## 
## Coefficients:
##                                                  Estimate Std. Error
## (Intercept)                                     4.489e+01  6.852e+00
## Edu_batchelors                                 -4.210e-01  1.315e-02
## `Persons/Household`                            -2.480e+00  4.041e-01
## Mormon                                         -2.518e-01  1.374e-02
## Romney                                         -2.929e-01  6.622e-02
## Black                                          -1.444e-01  9.071e-03
## Hispanic                                       -1.195e-01  6.662e-03
## Christian                                       2.357e-02  3.811e-03
## White                                           4.487e-02  8.625e-03
## Income                                          1.868e-04  3.063e-05
## `Median Value of Owner-Occupied Housing Units` -2.004e-05  1.322e-06
## `Travel Time to Work`                           5.907e-02  1.360e-02
## `% Female 2014`                                -1.231e-01  2.621e-02
## votes_gop_2012                                 -1.663e-05  4.492e-06
## `Nonemployer Establishments - 2013`             2.898e-05  5.693e-06
## Edu_highschool                                 -5.161e-02  1.467e-02
## `Land Area (in sq miles)`                      -1.710e-04  4.487e-05
## `Manufacturers Shipments - 2007`               -5.789e-08  1.954e-08
## `Persons Under 5`                              -2.449e-01  6.499e-02
## Other_Religion                                  4.432e-02  1.730e-02
## `Merchant Wholesaler Sales - 2007`              7.415e-08  1.927e-08
## Orthodox                                        6.386e-01  2.859e-01
## `Median Household Income`                       5.861e-05  1.502e-05
## Poverty                                         5.837e-02  1.788e-02
## `Hispanic-Owned Firms`                         -2.763e-02  1.164e-02
## `Private Nonfarm Employment`                   -8.215e-06  1.956e-06
## Density                                         1.256e-04  4.285e-05
## Veterans                                        2.484e-05  1.231e-05
## Jewish                                          6.635e-01  2.557e-01
## `Housing Units in Multi-Unit Structures`       -3.147e-02  1.198e-02
## Obama                                          -1.031e-01  6.684e-02
## `Accommodation and Food Service Sales - 2007`   2.106e-07  1.211e-07
## `Black-Owned Firms`                             1.669e-02  1.032e-02
## `Homeownership Rate`                           -1.999e-02  1.262e-02
## `% Change - Private Nonfarm Employment`        -1.211e-02  8.407e-03
##                                                t value Pr(>|t|)    
## (Intercept)                                      6.550 6.71e-11 ***
## Edu_batchelors                                 -32.004  < 2e-16 ***
## `Persons/Household`                             -6.136 9.54e-10 ***
## Mormon                                         -18.325  < 2e-16 ***
## Romney                                          -4.423 1.01e-05 ***
## Black                                          -15.920  < 2e-16 ***
## Hispanic                                       -17.940  < 2e-16 ***
## Christian                                        6.185 7.04e-10 ***
## White                                            5.203 2.09e-07 ***
## Income                                           6.099 1.20e-09 ***
## `Median Value of Owner-Occupied Housing Units` -15.156  < 2e-16 ***
## `Travel Time to Work`                            4.343 1.45e-05 ***
## `% Female 2014`                                 -4.696 2.77e-06 ***
## votes_gop_2012                                  -3.702 0.000217 ***
## `Nonemployer Establishments - 2013`              5.090 3.79e-07 ***
## Edu_highschool                                  -3.518 0.000441 ***
## `Land Area (in sq miles)`                       -3.810 0.000142 ***
## `Manufacturers Shipments - 2007`                -2.963 0.003066 ** 
## `Persons Under 5`                               -3.768 0.000168 ***
## Other_Religion                                   2.562 0.010451 *  
## `Merchant Wholesaler Sales - 2007`               3.847 0.000122 ***
## Orthodox                                         2.233 0.025594 *  
## `Median Household Income`                        3.902 9.75e-05 ***
## Poverty                                          3.265 0.001107 ** 
## `Hispanic-Owned Firms`                          -2.374 0.017678 *  
## `Private Nonfarm Employment`                    -4.199 2.76e-05 ***
## Density                                          2.930 0.003410 ** 
## Veterans                                         2.018 0.043691 *  
## Jewish                                           2.595 0.009512 ** 
## `Housing Units in Multi-Unit Structures`        -2.628 0.008643 ** 
## Obama                                           -1.543 0.122909    
## `Accommodation and Food Service Sales - 2007`    1.739 0.082180 .  
## `Black-Owned Firms`                              1.618 0.105734    
## `Homeownership Rate`                            -1.584 0.113403    
## `% Change - Private Nonfarm Employment`         -1.440 0.149921    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.713 on 3074 degrees of freedom
## Multiple R-squared:  0.7663, Adjusted R-squared:  0.7637 
## F-statistic: 296.4 on 34 and 3074 DF,  p-value: < 2.2e-16
votes$model_error_TR = (votes$Trump_Romney - votes$TR_Dev_Pred)

ME_TR = votes[,c(1,105)]
colnames(ME_TR) = c("region","value")
ME_TR$value = cut(ME_TR$value, breaks = c(-25,-10,-5,-1,1,5,10,Inf))

c= CountyChoropleth$new(ME_TR)
c$title = "Model Deviation: Trump-Romney"
c$add_state_outline = TRUE
c$legend = "Model Deviation"
c$set_num_colors(7)
c$ggplot_scale = scale_fill_manual(values=c("darkblue","dodgerblue","lightcyan","white","indianred1","red","firebrick4"))
county_ME_TR = c$render() + theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 48301, 2180, 2188, 2240, 2090,
## 2198, 15005, 2100, 2170, 51515, 2016, 2060, 2290, 2282, 8014, 32009, 2070,
## 2110, 2130, 2185, 2195, 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
county_ME_TR

Deviation Graphics: Overall

summary(Overall_Dev)
## 
## Call:
## lm(formula = per_shift ~ Edu_batchelors + `Persons/Household` + 
##     Romney + Black + Hispanic + Mormon + Christian + White + 
##     `Median Value of Owner-Occupied Housing Units` + `Median Household Income` + 
##     votes_gop_2012 + `Nonemployer Establishments - 2013` + `% Female 2014` + 
##     `Travel Time to Work` + Income + `Manufacturers Shipments - 2007` + 
##     `Merchant Wholesaler Sales - 2007` + `Hispanic-Owned Firms` + 
##     NonEnglish + `Land Area (in sq miles)` + `Private Nonfarm Employment` + 
##     Density + Poverty + `Accommodation and Food Service Sales - 2007` + 
##     Other_Religion + Orthodox + `Homeownership Rate`, data = Overall_Dev_Predict)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -29.1120  -2.7512   0.0554   2.8877  21.0307 
## 
## Coefficients:
##                                                  Estimate Std. Error
## (Intercept)                                     6.269e+01  3.142e+00
## Edu_batchelors                                 -7.741e-01  2.015e-02
## `Persons/Household`                            -3.785e+00  6.466e-01
## Romney                                         -4.155e-01  8.061e-03
## Black                                          -3.153e-01  1.411e-02
## Hispanic                                       -1.862e-01  1.892e-02
## Mormon                                         -2.889e-01  2.334e-02
## Christian                                       3.595e-02  6.439e-03
## White                                           8.760e-02  1.430e-02
## `Median Value of Owner-Occupied Housing Units` -3.547e-05  2.159e-06
## `Median Household Income`                       9.860e-05  2.498e-05
## votes_gop_2012                                 -1.997e-05  5.055e-06
## `Nonemployer Establishments - 2013`             5.304e-05  9.717e-06
## `% Female 2014`                                -3.182e-01  4.291e-02
## `Travel Time to Work`                           7.542e-02  2.219e-02
## Income                                          2.528e-04  5.160e-05
## `Manufacturers Shipments - 2007`               -1.233e-07  3.324e-08
## `Merchant Wholesaler Sales - 2007`              1.245e-07  3.016e-08
## `Hispanic-Owned Firms`                         -6.407e-02  1.994e-02
## NonEnglish                                     -7.185e-02  2.352e-02
## `Land Area (in sq miles)`                      -1.827e-04  7.431e-05
## `Private Nonfarm Employment`                   -1.250e-05  3.229e-06
## Density                                         2.107e-04  6.942e-05
## Poverty                                         6.461e-02  2.886e-02
## `Accommodation and Food Service Sales - 2007`   4.275e-07  2.040e-07
## Other_Religion                                  6.023e-02  2.956e-02
## Orthodox                                        9.071e-01  4.852e-01
## `Homeownership Rate`                            2.590e-02  1.619e-02
##                                                t value Pr(>|t|)    
## (Intercept)                                     19.955  < 2e-16 ***
## Edu_batchelors                                 -38.412  < 2e-16 ***
## `Persons/Household`                             -5.854 5.29e-09 ***
## Romney                                         -51.544  < 2e-16 ***
## Black                                          -22.345  < 2e-16 ***
## Hispanic                                        -9.842  < 2e-16 ***
## Mormon                                         -12.378  < 2e-16 ***
## Christian                                        5.583 2.57e-08 ***
## White                                            6.128 1.00e-09 ***
## `Median Value of Owner-Occupied Housing Units` -16.429  < 2e-16 ***
## `Median Household Income`                        3.947 8.09e-05 ***
## votes_gop_2012                                  -3.950 7.99e-05 ***
## `Nonemployer Establishments - 2013`              5.459 5.17e-08 ***
## `% Female 2014`                                 -7.415 1.57e-13 ***
## `Travel Time to Work`                            3.398 0.000687 ***
## Income                                           4.899 1.01e-06 ***
## `Manufacturers Shipments - 2007`                -3.710 0.000211 ***
## `Merchant Wholesaler Sales - 2007`               4.128 3.76e-05 ***
## `Hispanic-Owned Firms`                          -3.213 0.001326 ** 
## NonEnglish                                      -3.055 0.002272 ** 
## `Land Area (in sq miles)`                       -2.458 0.014022 *  
## `Private Nonfarm Employment`                    -3.871 0.000111 ***
## Density                                          3.035 0.002428 ** 
## Poverty                                          2.239 0.025224 *  
## `Accommodation and Food Service Sales - 2007`    2.096 0.036164 *  
## Other_Religion                                   2.037 0.041696 *  
## Orthodox                                         1.869 0.061659 .  
## `Homeownership Rate`                             1.600 0.109715    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.663 on 3081 degrees of freedom
## Multiple R-squared:  0.7946, Adjusted R-squared:  0.7928 
## F-statistic: 441.6 on 27 and 3081 DF,  p-value: < 2.2e-16
votes$model_error_overall = (votes$per_shift - votes$Overall_Dev_Pred)

ME_Overall = votes[,c(1,106)]
colnames(ME_Overall) = c("region","value")
ME_Overall$value = cut(ME_Overall$value, breaks = c(-30,-10,-5,-1,1,5,10,Inf))

c= CountyChoropleth$new(ME_Overall)
c$title = "Model Deviation: 2016 Election Results"
c$add_state_outline = TRUE
c$legend = "Model Deviation"
c$set_num_colors(7)
c$ggplot_scale = scale_fill_manual(values=c("darkblue","dodgerblue","lightcyan","white","indianred1","red","firebrick4"))
county_ME_Overall = c$render() + theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 48301, 2180, 2188, 2240, 2090,
## 2198, 15005, 2100, 2170, 51515, 2016, 2060, 2290, 2282, 8014, 32009, 2070,
## 2110, 2130, 2185, 2195, 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
county_ME_Overall

Predict Trump and Clinton percentage

Predict_Clinton = votes[,c(8,19,20,26:28,30:35,40,42:63,67:76,83:95,100)]
Predict_Trump = votes[,c(9,19,20,26:28,30:35,40,42:63,67:76,83:95,100)]

Predict_Clinton = na.omit(Predict_Clinton)
Predict_Trump = na.omit(Predict_Trump)

#Clinton
null_Clinton = lm(Clinton~1,data = Predict_Clinton)
full_Clinton = lm(Clinton~.,data = Predict_Clinton)
Clinton_Dev = step(null_Clinton,scope=list(upper=full_Clinton),data=Predict_Clinton,direction="both")
votes$Clinton_Percent_Predict = predict(Clinton_Dev,votes)

#Trump
null_Trump = lm(Trump~1,data = Predict_Trump)
full_Trump = lm(Trump~.,data = Predict_Trump)
Trump_Dev = step(null_Trump,scope=list(upper=full_Trump),data=Predict_Trump,direction="both")
votes$Trump_Percent_Predict = predict(Trump_Dev,votes)

Analyze Predict Percentage

#Clinton
summary(Clinton_Dev)
## 
## Call:
## lm(formula = Clinton ~ Obama + `Foreign Born` + Black + Edu_batchelors + 
##     NonEnglish + Protestant + `% Female 2014` + `Median Value of Owner-Occupied Housing Units` + 
##     Income + Hispanic + White + Veterans + `Nonemployer Establishments - 2013` + 
##     `Manufacturers Shipments - 2007` + Edu_highschool + `Hispanic-Owned Firms` + 
##     Mormon + `Persons Under 18` + Density + `Homeownership Rate` + 
##     `Persons/Household` + `Median Household Income` + `Living in Same House 1+ Years` + 
##     `Merchant Wholesaler Sales - 2007` + `Private Nonfarm Employment` + 
##     population_change + `Accommodation and Food Service Sales - 2007` + 
##     `Travel Time to Work` + Catholic + `Persons Under 5`, data = Predict_Clinton)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -9.1088 -1.3843  0.0161  1.3411 13.5568 
## 
## Coefficients:
##                                                  Estimate Std. Error
## (Intercept)                                    -1.127e+01  1.928e+00
## Obama                                           7.770e-01  4.073e-03
## `Foreign Born`                                  3.118e-02  1.725e-02
## Black                                           1.686e-01  6.925e-03
## Edu_batchelors                                  3.445e-01  1.054e-02
## NonEnglish                                      5.385e-02  1.402e-02
## Protestant                                     -2.949e-02  5.330e-03
## `% Female 2014`                                 2.101e-01  2.300e-02
## `Median Value of Owner-Occupied Housing Units`  1.338e-05  1.108e-06
## Income                                         -7.167e-05  2.523e-05
## Hispanic                                        7.556e-02  9.076e-03
## White                                          -4.592e-02  6.750e-03
## Veterans                                        1.770e-05  6.692e-06
## `Nonemployer Establishments - 2013`            -2.429e-05  4.745e-06
## `Manufacturers Shipments - 2007`                6.367e-08  1.590e-08
## Edu_highschool                                 -4.245e-02  1.210e-02
## `Hispanic-Owned Firms`                          3.479e-02  9.615e-03
## Mormon                                          4.558e-02  1.110e-02
## `Persons Under 18`                             -8.695e-02  3.220e-02
## Density                                        -1.292e-04  3.320e-05
## `Homeownership Rate`                           -3.152e-02  9.264e-03
## `Persons/Household`                             1.545e+00  3.418e-01
## `Median Household Income`                      -2.617e-05  1.150e-05
## `Living in Same House 1+ Years`                 3.951e-02  1.336e-02
## `Merchant Wholesaler Sales - 2007`             -5.285e-08  1.539e-08
## `Private Nonfarm Employment`                    5.876e-06  1.595e-06
## population_change                               3.285e-02  1.246e-02
## `Accommodation and Food Service Sales - 2007`  -2.008e-07  9.743e-08
## `Travel Time to Work`                          -2.642e-02  1.112e-02
## Catholic                                       -1.074e-02  5.788e-03
## `Persons Under 5`                              -1.306e-01  8.055e-02
##                                                t value Pr(>|t|)    
## (Intercept)                                     -5.843 5.68e-09 ***
## Obama                                          190.755  < 2e-16 ***
## `Foreign Born`                                   1.807 0.070821 .  
## Black                                           24.352  < 2e-16 ***
## Edu_batchelors                                  32.671  < 2e-16 ***
## NonEnglish                                       3.840 0.000126 ***
## Protestant                                      -5.532 3.43e-08 ***
## `% Female 2014`                                  9.132  < 2e-16 ***
## `Median Value of Owner-Occupied Housing Units`  12.071  < 2e-16 ***
## Income                                          -2.841 0.004523 ** 
## Hispanic                                         8.326  < 2e-16 ***
## White                                           -6.803 1.23e-11 ***
## Veterans                                         2.645 0.008213 ** 
## `Nonemployer Establishments - 2013`             -5.120 3.24e-07 ***
## `Manufacturers Shipments - 2007`                 4.005 6.36e-05 ***
## Edu_highschool                                  -3.507 0.000460 ***
## `Hispanic-Owned Firms`                           3.619 0.000301 ***
## Mormon                                           4.107 4.11e-05 ***
## `Persons Under 18`                              -2.700 0.006968 ** 
## Density                                         -3.891 0.000102 ***
## `Homeownership Rate`                            -3.402 0.000677 ***
## `Persons/Household`                              4.520 6.41e-06 ***
## `Median Household Income`                       -2.276 0.022889 *  
## `Living in Same House 1+ Years`                  2.957 0.003130 ** 
## `Merchant Wholesaler Sales - 2007`              -3.435 0.000601 ***
## `Private Nonfarm Employment`                     3.684 0.000234 ***
## population_change                                2.637 0.008410 ** 
## `Accommodation and Food Service Sales - 2007`   -2.061 0.039420 *  
## `Travel Time to Work`                           -2.377 0.017533 *  
## Catholic                                        -1.855 0.063671 .  
## `Persons Under 5`                               -1.622 0.104997    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.217 on 3078 degrees of freedom
## Multiple R-squared:  0.9793, Adjusted R-squared:  0.9791 
## F-statistic:  4866 on 30 and 3078 DF,  p-value: < 2.2e-16
Clinton_Deviation = data.frame(votes[,1])
Clinton_Deviation$deviation = votes$Clinton - votes$Clinton_Percent_Predict
colnames(Clinton_Deviation) = c("region", "value")
Clinton_Deviation$value = cut(Clinton_Deviation$value, breaks = c(-10,-5,-1,1,5,10,Inf))

c= CountyChoropleth$new(Clinton_Deviation)
c$title = "Clinton Percentage Deviation"
c$add_state_outline = TRUE
c$legend = "Model Deviation"
c$set_num_colors(7)
c$ggplot_scale = scale_fill_manual(values=c("red","indianred1","white","lightcyan1","dodgerblue","darkblue"))
county_Clinton_Dev = c$render() + theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 48301, 2180, 2188, 2240, 2090,
## 2198, 15005, 2100, 2170, 51515, 2016, 2060, 2290, 2282, 8014, 32009, 2070,
## 2110, 2130, 2185, 2195, 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
county_Clinton_Dev

#Trump
summary(Trump_Dev)
## 
## Call:
## lm(formula = Trump ~ Obama + Edu_batchelors + `Persons/Household` + 
##     Black + Mormon + Hispanic + Romney + Christian + White + 
##     Income + `Median Value of Owner-Occupied Housing Units` + 
##     `Travel Time to Work` + `% Female 2014` + Edu_highschool + 
##     `Nonemployer Establishments - 2013` + `Manufacturers Shipments - 2007` + 
##     `Land Area (in sq miles)` + `Persons Under 5` + Other_Religion + 
##     `Hispanic-Owned Firms` + Orthodox + `Merchant Wholesaler Sales - 2007` + 
##     `Private Nonfarm Employment` + Density + `Accommodation and Food Service Sales - 2007` + 
##     `Median Household Income` + Poverty + `Housing Units in Multi-Unit Structures` + 
##     Jewish + `Homeownership Rate` + `Black-Owned Firms` + `% Change - Private Nonfarm Employment`, 
##     data = Predict_Trump)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -21.899  -1.461   0.013   1.619  18.015 
## 
## Coefficients:
##                                                  Estimate Std. Error
## (Intercept)                                     4.825e+01  6.799e+00
## Obama                                          -1.217e-01  6.669e-02
## Edu_batchelors                                 -4.294e-01  1.296e-02
## `Persons/Household`                            -2.591e+00  4.034e-01
## Black                                          -1.464e-01  9.070e-03
## Mormon                                         -2.502e-01  1.375e-02
## Hispanic                                       -1.182e-01  6.654e-03
## Romney                                          6.852e-01  6.600e-02
## Christian                                       2.424e-02  3.802e-03
## White                                           4.403e-02  8.616e-03
## Income                                          1.892e-04  3.068e-05
## `Median Value of Owner-Occupied Housing Units` -1.938e-05  1.314e-06
## `Travel Time to Work`                           5.505e-02  1.347e-02
## `% Female 2014`                                -1.309e-01  2.614e-02
## Edu_highschool                                 -5.089e-02  1.464e-02
## `Nonemployer Establishments - 2013`             2.827e-05  5.646e-06
## `Manufacturers Shipments - 2007`               -6.086e-08  1.935e-08
## `Land Area (in sq miles)`                      -1.813e-04  4.458e-05
## `Persons Under 5`                              -2.393e-01  6.508e-02
## Other_Religion                                  4.307e-02  1.732e-02
## `Hispanic-Owned Firms`                         -2.987e-02  1.161e-02
## Orthodox                                        6.061e-01  2.860e-01
## `Merchant Wholesaler Sales - 2007`              6.665e-08  1.748e-08
## `Private Nonfarm Employment`                   -9.670e-06  1.749e-06
## Density                                         1.674e-04  3.937e-05
## `Accommodation and Food Service Sales - 2007`   2.676e-07  1.191e-07
## `Median Household Income`                       5.470e-05  1.501e-05
## Poverty                                         5.698e-02  1.788e-02
## `Housing Units in Multi-Unit Structures`       -3.918e-02  1.178e-02
## Jewish                                          5.920e-01  2.555e-01
## `Homeownership Rate`                           -2.515e-02  1.256e-02
## `Black-Owned Firms`                             1.810e-02  1.031e-02
## `% Change - Private Nonfarm Employment`        -1.259e-02  8.423e-03
##                                                t value Pr(>|t|)    
## (Intercept)                                      7.096 1.59e-12 ***
## Obama                                           -1.825 0.068057 .  
## Edu_batchelors                                 -33.139  < 2e-16 ***
## `Persons/Household`                             -6.423 1.54e-10 ***
## Black                                          -16.139  < 2e-16 ***
## Mormon                                         -18.196  < 2e-16 ***
## Hispanic                                       -17.763  < 2e-16 ***
## Romney                                          10.382  < 2e-16 ***
## Christian                                        6.376 2.09e-10 ***
## White                                            5.110 3.41e-07 ***
## Income                                           6.168 7.82e-10 ***
## `Median Value of Owner-Occupied Housing Units` -14.752  < 2e-16 ***
## `Travel Time to Work`                            4.086 4.50e-05 ***
## `% Female 2014`                                 -5.008 5.80e-07 ***
## Edu_highschool                                  -3.477 0.000514 ***
## `Nonemployer Establishments - 2013`              5.008 5.81e-07 ***
## `Manufacturers Shipments - 2007`                -3.145 0.001677 ** 
## `Land Area (in sq miles)`                       -4.068 4.86e-05 ***
## `Persons Under 5`                               -3.677 0.000240 ***
## Other_Religion                                   2.487 0.012946 *  
## `Hispanic-Owned Firms`                          -2.573 0.010135 *  
## Orthodox                                         2.119 0.034150 *  
## `Merchant Wholesaler Sales - 2007`               3.813 0.000140 ***
## `Private Nonfarm Employment`                    -5.529 3.49e-08 ***
## Density                                          4.251 2.19e-05 ***
## `Accommodation and Food Service Sales - 2007`    2.247 0.024742 *  
## `Median Household Income`                        3.643 0.000274 ***
## Poverty                                          3.186 0.001455 ** 
## `Housing Units in Multi-Unit Structures`        -3.324 0.000897 ***
## Jewish                                           2.317 0.020544 *  
## `Homeownership Rate`                            -2.002 0.045351 *  
## `Black-Owned Firms`                              1.756 0.079248 .  
## `% Change - Private Nonfarm Employment`         -1.494 0.135179    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.719 on 3076 degrees of freedom
## Multiple R-squared:  0.9701, Adjusted R-squared:  0.9698 
## F-statistic:  3119 on 32 and 3076 DF,  p-value: < 2.2e-16
Trump_Deviation = data.frame(votes[,1])
Trump_Deviation$deviation = votes$Trump - votes$Trump_Percent_Predict
colnames(Trump_Deviation) = c("region", "value")
Trump_Deviation$value = cut(Trump_Deviation$value, breaks = c(-25,-10,-5,-1,1,5,10,Inf))

c= CountyChoropleth$new(Trump_Deviation)
c$title = "Trump Percentage Deviation"
c$add_state_outline = TRUE
c$legend = "Model Deviation"
c$set_num_colors(7)
c$ggplot_scale = scale_fill_manual(values=c("darkblue","dodgerblue","lightcyan","white","indianred1","red","firebrick4"))
county_Trump_Dev = c$render() + theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: 2050, 2105, 2122, 2150, 2164, 48301, 2180, 2188, 2240, 2090,
## 2198, 15005, 2100, 2170, 51515, 2016, 2060, 2290, 2282, 8014, 32009, 2070,
## 2110, 2130, 2185, 2195, 2220, 2230, 2020, 2068, 2013, 2261, 2270, 2275
county_Trump_Dev

Predict Total Votes

Predict_Votes_Clinton = votes[,c(5,14:16,19,20,26:28,30:35,40,42:63,67:76,83:95,100)]
Predict_Votes_Trump = votes[,c(6,14:16,19,20,26:28,30:35,40,42:63,67:76,83:95,100)]

Predict_Votes_Clinton = na.omit(Predict_Votes_Clinton)
Predict_Votes_Trump = na.omit(Predict_Votes_Trump)

#Clinton
null_Votes_Clinton = lm(votes_dem_2016~1,data = Predict_Votes_Clinton)
full_Votes_Clinton = lm(votes_dem_2016~.,data = Predict_Votes_Clinton)
Clinton_Votes_Dev = step(null_Votes_Clinton,scope=list(upper=full_Votes_Clinton),data=Predict_Votes_Clinton,direction="both")
votes$Clinton_Votes_Predict = predict(Clinton_Votes_Dev,votes)

#Trump
null_Votes_Trump = lm(votes_gop_2016~1,data = Predict_Votes_Trump)
full_Votes_Trump = lm(votes_gop_2016~.,data = Predict_Votes_Trump)
Trump_Votes_Dev = step(null_Votes_Trump,scope=list(upper=full_Votes_Trump),data=Predict_Votes_Trump,direction="both")
votes$Trump_Votes_Predict = predict(Trump_Votes_Dev,votes)

Analyze Total Votes

summary(Clinton_Votes_Dev)
## 
## Call:
## lm(formula = votes_dem_2016 ~ votes_dem_2012 + `Nonemployer Establishments - 2013` + 
##     `Private Nonfarm Employment` + votes_gop_2012 + `Foreign Born` + 
##     `Manufacturers Shipments - 2007` + total_votes_2012 + `Housing Units 2014` + 
##     population2014 + population2010 + `Total Number of Firms` + 
##     Veterans + age65plus + Households + `Median Value of Owner-Occupied Housing Units` + 
##     `Housing Units in Multi-Unit Structures` + `Merchant Wholesaler Sales - 2007` + 
##     Hindu + Buddhist + Obama + Poverty + `Median Household Income` + 
##     population_change + White + Density + `Hispanic-Owned Firms` + 
##     `Persons Under 18` + Orthodox + Hispanic + NonEnglish + `Retail Sales - 2007` + 
##     `Private Nonfarm Establishments 2013` + Protestant, data = Predict_Votes_Clinton)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -60622   -577    115    698  67806 
## 
## Coefficients:
##                                                  Estimate Std. Error
## (Intercept)                                    -8.581e+02  1.813e+03
## votes_dem_2012                                  1.738e+00  1.473e-01
## `Nonemployer Establishments - 2013`            -5.240e-01  7.061e-02
## `Private Nonfarm Employment`                    2.142e-02  4.849e-03
## votes_gop_2012                                  7.056e-01  1.459e-01
## `Foreign Born`                                  1.002e+02  3.153e+01
## `Manufacturers Shipments - 2007`               -8.132e-05  3.297e-05
## total_votes_2012                               -7.815e-01  1.463e-01
## `Housing Units 2014`                           -1.449e-01  1.476e-02
## population2014                                  4.417e-01  1.416e-02
## population2010                                 -4.341e-01  1.449e-02
## `Total Number of Firms`                         6.543e-01  8.445e-02
## Veterans                                       -2.424e-01  2.650e-02
## age65plus                                       9.401e+01  2.860e+01
## Households                                      1.656e-01  2.580e-02
## `Median Value of Owner-Occupied Housing Units`  9.597e-03  1.947e-03
## `Housing Units in Multi-Unit Structures`       -3.756e+01  1.394e+01
## `Merchant Wholesaler Sales - 2007`             -1.335e-04  3.143e-05
## Hindu                                           2.981e+03  7.133e+02
## Buddhist                                       -2.497e+03  6.305e+02
## Obama                                          -6.072e+01  7.643e+00
## Poverty                                         1.125e+02  2.450e+01
## `Median Household Income`                       6.184e-02  1.672e-02
## population_change                              -9.928e+01  2.276e+01
## White                                          -3.157e+01  6.946e+00
## Density                                        -3.065e-01  7.859e-02
## `Hispanic-Owned Firms`                          5.612e+01  1.759e+01
## `Persons Under 18`                             -9.458e+01  3.250e+01
## Orthodox                                       -9.151e+02  4.279e+02
## Hispanic                                       -4.545e+01  1.597e+01
## NonEnglish                                      4.866e+01  2.276e+01
## `Retail Sales - 2007`                          -3.713e-04  1.599e-04
## `Private Nonfarm Establishments 2013`           2.970e-01  1.730e-01
## Protestant                                      1.407e+01  9.408e+00
##                                                t value Pr(>|t|)    
## (Intercept)                                     -0.473 0.636044    
## votes_dem_2012                                  11.798  < 2e-16 ***
## `Nonemployer Establishments - 2013`             -7.421 1.50e-13 ***
## `Private Nonfarm Employment`                     4.417 1.04e-05 ***
## votes_gop_2012                                   4.834 1.40e-06 ***
## `Foreign Born`                                   3.178 0.001500 ** 
## `Manufacturers Shipments - 2007`                -2.466 0.013703 *  
## total_votes_2012                                -5.341 9.94e-08 ***
## `Housing Units 2014`                            -9.821  < 2e-16 ***
## population2014                                  31.186  < 2e-16 ***
## population2010                                 -29.948  < 2e-16 ***
## `Total Number of Firms`                          7.748 1.26e-14 ***
## Veterans                                        -9.149  < 2e-16 ***
## age65plus                                        3.288 0.001021 ** 
## Households                                       6.418 1.60e-10 ***
## `Median Value of Owner-Occupied Housing Units`   4.930 8.66e-07 ***
## `Housing Units in Multi-Unit Structures`        -2.695 0.007081 ** 
## `Merchant Wholesaler Sales - 2007`              -4.247 2.23e-05 ***
## Hindu                                            4.179 3.01e-05 ***
## Buddhist                                        -3.961 7.62e-05 ***
## Obama                                           -7.944 2.72e-15 ***
## Poverty                                          4.593 4.54e-06 ***
## `Median Household Income`                        3.698 0.000221 ***
## population_change                               -4.362 1.33e-05 ***
## White                                           -4.545 5.69e-06 ***
## Density                                         -3.901 9.80e-05 ***
## `Hispanic-Owned Firms`                           3.190 0.001435 ** 
## `Persons Under 18`                              -2.910 0.003634 ** 
## Orthodox                                        -2.138 0.032560 *  
## Hispanic                                        -2.846 0.004460 ** 
## NonEnglish                                       2.138 0.032636 *  
## `Retail Sales - 2007`                           -2.321 0.020326 *  
## `Private Nonfarm Establishments 2013`            1.717 0.086111 .  
## Protestant                                       1.495 0.134972    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4042 on 3075 degrees of freedom
## Multiple R-squared:  0.9969, Adjusted R-squared:  0.9968 
## F-statistic: 2.974e+04 on 33 and 3075 DF,  p-value: < 2.2e-16
summary(Trump_Votes_Dev)
## 
## Call:
## lm(formula = votes_gop_2016 ~ votes_gop_2012 + `Private Nonfarm Employment` + 
##     `Housing Units 2014` + Density + population2014 + `Private Nonfarm Establishments 2013` + 
##     population2010 + total_votes_2012 + votes_dem_2012 + `Total Number of Firms` + 
##     `Accommodation and Food Service Sales - 2007` + `Building Permits` + 
##     Orthodox + Black + Mormon + population_change + `Persons Under 5` + 
##     Edu_batchelors + Romney + `Median Household Income` + `% Female 2014` + 
##     age65plus + `Median Value of Owner-Occupied Housing Units` + 
##     `Foreign Born` + `Persons Under 18` + Veterans + Hispanic + 
##     Protestant + Catholic + `Hispanic-Owned Firms` + `Manufacturers Shipments - 2007` + 
##     Women + `Black-Owned Firms` + Income + `Retail Sales - 2007` + 
##     Jewish, data = Predict_Votes_Trump)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -54815   -862   -118    659  45629 
## 
## Coefficients:
##                                                  Estimate Std. Error
## (Intercept)                                    -2.573e+03  1.809e+03
## votes_gop_2012                                  3.170e+00  1.260e-01
## `Private Nonfarm Employment`                   -1.158e-01  4.580e-03
## `Housing Units 2014`                            7.389e-02  1.001e-02
## Density                                         3.893e-01  6.653e-02
## population2014                                 -2.139e-01  1.645e-02
## `Private Nonfarm Establishments 2013`           2.026e+00  1.523e-01
## population2010                                  2.159e-01  1.696e-02
## total_votes_2012                               -2.196e+00  1.258e-01
## votes_dem_2012                                  2.166e+00  1.273e-01
## `Total Number of Firms`                        -2.340e-01  3.303e-02
## `Accommodation and Food Service Sales - 2007`   1.694e-03  1.723e-04
## `Building Permits`                              1.336e+00  1.927e-01
## Orthodox                                        1.898e+03  4.000e+02
## Black                                          -2.860e+01  7.420e+00
## Mormon                                         -4.510e+01  1.856e+01
## population_change                               1.433e+02  2.183e+01
## `Persons Under 5`                              -4.508e+02  1.293e+02
## Edu_batchelors                                 -9.937e+01  1.587e+01
## Romney                                         -2.268e+01  6.733e+00
## `Median Household Income`                       1.041e-01  1.832e-02
## `% Female 2014`                                 2.406e+02  4.065e+01
## age65plus                                      -6.880e+01  2.838e+01
## `Median Value of Owner-Occupied Housing Units` -1.056e-02  1.899e-03
## `Foreign Born`                                  1.176e+02  2.523e+01
## `Persons Under 18`                             -1.391e+02  5.282e+01
## Veterans                                        9.149e-02  2.480e-02
## Hispanic                                       -3.934e+01  9.977e+00
## Protestant                                     -2.186e+01  8.846e+00
## Catholic                                        2.659e+01  9.652e+00
## `Hispanic-Owned Firms`                          3.804e+01  1.620e+01
## `Manufacturers Shipments - 2007`                6.798e-05  2.974e-05
## Women                                           1.236e+01  6.169e+00
## `Black-Owned Firms`                            -2.780e+01  1.427e+01
## Income                                         -8.685e-02  4.101e-02
## `Retail Sales - 2007`                          -2.510e-04  1.454e-04
## Jewish                                          6.189e+02  3.626e+02
##                                                t value Pr(>|t|)    
## (Intercept)                                     -1.422 0.155084    
## votes_gop_2012                                  25.160  < 2e-16 ***
## `Private Nonfarm Employment`                   -25.294  < 2e-16 ***
## `Housing Units 2014`                             7.380 2.03e-13 ***
## Density                                          5.852 5.36e-09 ***
## population2014                                 -13.004  < 2e-16 ***
## `Private Nonfarm Establishments 2013`           13.301  < 2e-16 ***
## population2010                                  12.729  < 2e-16 ***
## total_votes_2012                               -17.459  < 2e-16 ***
## votes_dem_2012                                  17.019  < 2e-16 ***
## `Total Number of Firms`                         -7.086 1.70e-12 ***
## `Accommodation and Food Service Sales - 2007`    9.831  < 2e-16 ***
## `Building Permits`                               6.933 5.02e-12 ***
## Orthodox                                         4.745 2.18e-06 ***
## Black                                           -3.854 0.000119 ***
## Mormon                                          -2.430 0.015148 *  
## population_change                                6.564 6.13e-11 ***
## `Persons Under 5`                               -3.485 0.000499 ***
## Edu_batchelors                                  -6.260 4.38e-10 ***
## Romney                                          -3.368 0.000767 ***
## `Median Household Income`                        5.681 1.46e-08 ***
## `% Female 2014`                                  5.920 3.57e-09 ***
## age65plus                                       -2.425 0.015376 *  
## `Median Value of Owner-Occupied Housing Units`  -5.560 2.93e-08 ***
## `Foreign Born`                                   4.663 3.25e-06 ***
## `Persons Under 18`                              -2.633 0.008511 ** 
## Veterans                                         3.689 0.000229 ***
## Hispanic                                        -3.943 8.22e-05 ***
## Protestant                                      -2.471 0.013531 *  
## Catholic                                         2.755 0.005904 ** 
## `Hispanic-Owned Firms`                           2.349 0.018903 *  
## `Manufacturers Shipments - 2007`                 2.286 0.022331 *  
## Women                                            2.004 0.045186 *  
## `Black-Owned Firms`                             -1.947 0.051589 .  
## Income                                          -2.118 0.034252 *  
## `Retail Sales - 2007`                           -1.726 0.084409 .  
## Jewish                                           1.707 0.087898 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3768 on 3072 degrees of freedom
## Multiple R-squared:  0.9914, Adjusted R-squared:  0.9913 
## F-statistic:  9828 on 36 and 3072 DF,  p-value: < 2.2e-16
state_predict = data.frame(states[,c(1,10,11)])
state_predict$gop_votes = 0
state_predict$dem_votes = 0
state_predict$winner = NA

for(i in seq(1:dim(votes)[1])){
  for(j in seq(1:dim(state_predict)[1])){
    if(votes[i,12] == state_predict[j,3] && !is.na(votes[i,109]) && !is.na(votes[i,110])){
      state_predict[j,4] = state_predict[j,4] + votes[i,110]
      state_predict[j,5] = state_predict[j,5] + votes[i,109]
    }
  }
}

for(i in seq(1:dim(state_predict)[1])){
  if(state_predict[i,4] > state_predict[i,5]){
    state_predict[i,6] = "TRUMP"
  }
  if(state_predict[i,4] < state_predict[i,5]){
    state_predict[i,6] = "CLINTON"
  }
  if(state_predict[i,4] == state_predict[i,5]){
    state_predict[i,6] = "TIE"
  }
}

colnames(state_predict)[2] = "region"
colnames(state_predict)[6] = "value"

state_predict$gop_margin = ((state_predict$gop_votes - state_predict$dem_votes) / (state_predict$gop_votes + state_predict$dem_votes)) * 100

c = StateChoropleth$new(state_predict)
c$title = "2016 Winner Predicted by Model"
c$add_state_outline = TRUE
c$legend = "Winner"
c$set_num_colors(3)
c$ggplot_scale = scale_fill_manual(values = c("blue","red","white"))
state_predict2 = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: alaska
state_predict2

state_margin = state_predict[,c(2,7)]
colnames(state_margin) = c("region", "value")
state_margin$value = cut(state_margin$value, breaks = c(-100,-10,-5,-1,1,5,10,100))

c = StateChoropleth$new(state_margin)
c$title = "2016 Winner Predicted by Model"
c$add_state_outline = TRUE
c$legend = "Model Predicted Win Margin"
c$set_num_colors(7)
c$ggplot_scale = scale_fill_manual(values=c("darkblue","dodgerblue","lightcyan","white","indianred1","red","firebrick4"))
state_predict3 = c$render() + 
              theme(legend.position = "right")
## Warning in self$bind(): The following regions were missing and are being
## set to NA: alaska
state_predict3

Correlation Analysis

library(corrplot)
## Warning: package 'corrplot' was built under R version 3.3.3
corr_subset = subset(votes, select=c(Trump,Clinton,Romney,Obama,population_change,White,Black,Hispanic,Income,Edu_highschool,Edu_batchelors))
corr_subset_religion = subset(votes, select=c(Trump,Clinton,Romney,Obama,Evangelical,Protestant,Catholic,Jewish,Mormon,Christian))

correlation = cor(corr_subset,use = "complete.obs")
correlation_religion = cor(corr_subset_religion,use = "complete.obs")


demographics = corrplot(correlation, method="shade", shade.col=NA, tl.col="black", tl.srt=45, addCoef.col="black", addcolorlabel="no")
## Warning in text.default(pos.xlabel[, 1], pos.xlabel[, 2], newcolnames, srt
## = tl.srt, : "addcolorlabel" is not a graphical parameter
## Warning in text.default(pos.ylabel[, 1], pos.ylabel[, 2], newrownames, col
## = tl.col, : "addcolorlabel" is not a graphical parameter
## Warning in title(title, ...): "addcolorlabel" is not a graphical parameter

religion = corrplot(correlation_religion, method="shade", shade.col=NA, tl.col="black", tl.srt=45, addCoef.col="black", addcolorlabel="no")
## Warning in text.default(pos.xlabel[, 1], pos.xlabel[, 2], newcolnames, srt
## = tl.srt, : "addcolorlabel" is not a graphical parameter
## Warning in text.default(pos.ylabel[, 1], pos.ylabel[, 2], newrownames, col
## = tl.col, : "addcolorlabel" is not a graphical parameter
## Warning in title(title, ...): "addcolorlabel" is not a graphical parameter

demographics
##                         Trump      Clinton       Romney       Obama
## Trump              1.00000000 -0.983706435  0.934274000 -0.93434018
## Clinton           -0.98370644  1.000000000 -0.941509781  0.94669692
## Romney             0.93427400 -0.941509781  1.000000000 -0.99813021
## Obama             -0.93434018  0.946696924 -0.998130214  1.00000000
## population_change -0.14387402  0.116002093 -0.005088279  0.00512069
## White              0.52966212 -0.593048979  0.478312388 -0.50034927
## Black             -0.42517294  0.509268989 -0.390921003  0.41515417
## Hispanic          -0.18842304  0.182939797 -0.079660815  0.08122373
## Income            -0.23666574  0.197489495 -0.129504155  0.12250369
## Edu_highschool    -0.09011927  0.007043676 -0.049315225  0.03366712
## Edu_batchelors    -0.48724875  0.434224236 -0.300027325  0.29265640
##                   population_change       White       Black    Hispanic
## Trump                  -0.143874022  0.52966212 -0.42517294 -0.18842304
## Clinton                 0.116002093 -0.59304898  0.50926899  0.18293980
## Romney                 -0.005088279  0.47831239 -0.39092100 -0.07966081
## Obama                   0.005120690 -0.50034927  0.41515417  0.08122373
## population_change       1.000000000 -0.01033989 -0.09887615  0.17060996
## White                  -0.010339887  1.00000000 -0.87293276  0.04397709
## Black                  -0.098876154 -0.87293276  1.00000000 -0.09528965
## Hispanic                0.170609962  0.04397709 -0.09528965  1.00000000
## Income                  0.383692512  0.20182088 -0.23822097 -0.03902452
## Edu_highschool          0.206248751  0.29455104 -0.35619057 -0.38413600
## Edu_batchelors          0.435806228 -0.00106647 -0.08473178  0.01336191
##                        Income Edu_highschool Edu_batchelors
## Trump             -0.23666574   -0.090119273    -0.48724875
## Clinton            0.19748949    0.007043676     0.43422424
## Romney            -0.12950415   -0.049315225    -0.30002733
## Obama              0.12250369    0.033667123     0.29265640
## population_change  0.38369251    0.206248751     0.43580623
## White              0.20182088    0.294551044    -0.00106647
## Black             -0.23822097   -0.356190573    -0.08473178
## Hispanic          -0.03902452   -0.384136004     0.01336191
## Income             1.00000000    0.642988316     0.78062981
## Edu_highschool     0.64298832    1.000000000     0.60138653
## Edu_batchelors     0.78062981    0.601386528     1.00000000
religion
##                   Trump     Clinton      Romney       Obama Evangelical
## Trump        1.00000000 -0.98369258  0.93423719 -0.93432101  0.23433745
## Clinton     -0.98369258  1.00000000 -0.94144501  0.94664018 -0.14437003
## Romney       0.93423719 -0.94144501  1.00000000 -0.99814090  0.21845482
## Obama       -0.93432101  0.94664018 -0.99814090  1.00000000 -0.19535489
## Evangelical  0.23433745 -0.14437003  0.21845482 -0.19535489  1.00000000
## Protestant   0.13208925 -0.14555522  0.04995778 -0.05100575 -0.04920175
## Catholic    -0.15322308  0.13026914 -0.17466183  0.17006743 -0.37589364
## Jewish      -0.33262451  0.33792807 -0.27718221  0.27882978 -0.17206418
## Mormon       0.01461194 -0.09757169  0.12890155 -0.13630309 -0.15133860
## Christian    0.14550766 -0.08575101  0.08054665 -0.06302473  0.62757409
##              Protestant    Catholic      Jewish      Mormon   Christian
## Trump        0.13208925 -0.15322308 -0.33262451  0.01461194  0.14550766
## Clinton     -0.14555522  0.13026914  0.33792807 -0.09757169 -0.08575101
## Romney       0.04995778 -0.17466183 -0.27718221  0.12890155  0.08054665
## Obama       -0.05100575  0.17006743  0.27882978 -0.13630309 -0.06302473
## Evangelical -0.04920175 -0.37589364 -0.17206418 -0.15133860  0.62757409
## Protestant   1.00000000  0.22831091 -0.06893755 -0.11001702  0.60529619
## Catholic     0.22831091  1.00000000  0.13279224 -0.06883001  0.29035959
## Jewish      -0.06893755  0.13279224  1.00000000 -0.02128366 -0.10046100
## Mormon      -0.11001702 -0.06883001 -0.02128366  1.00000000 -0.21864417
## Christian    0.60529619  0.29035959 -0.10046100 -0.21864417  1.00000000

Most Important Feature Analysis

Clinton

library(h2o)
## Warning: package 'h2o' was built under R version 3.3.3
## 
## ----------------------------------------------------------------------
## 
## Your next step is to start H2O:
##     > h2o.init()
## 
## For H2O package documentation, ask for help:
##     > ??h2o
## 
## After starting H2O, you can use the Web UI at http://localhost:54321
## For more information visit http://docs.h2o.ai
## 
## ----------------------------------------------------------------------
## 
## Attaching package: 'h2o'
## The following object is masked from 'package:acs':
## 
##     apply
## The following objects are masked from 'package:stats':
## 
##     cor, sd, var
## The following objects are masked from 'package:base':
## 
##     %*%, %in%, &&, ||, apply, as.factor, as.numeric, colnames,
##     colnames<-, ifelse, is.character, is.factor, is.numeric, log,
##     log10, log1p, log2, round, signif, trunc
h2o.init(nthreads=-1,max_mem_size='6G')
##  Connection successful!
## 
## R is connected to the H2O cluster: 
##     H2O cluster uptime:         3 hours 27 minutes 
##     H2O cluster version:        3.10.4.6 
##     H2O cluster version age:    1 month and 26 days  
##     H2O cluster name:           H2O_started_from_R_onest_awv997 
##     H2O cluster total nodes:    1 
##     H2O cluster total memory:   4.91 GB 
##     H2O cluster total cores:    4 
##     H2O cluster allowed cores:  4 
##     H2O cluster healthy:        TRUE 
##     H2O Connection ip:          localhost 
##     H2O Connection port:        54321 
##     H2O Connection proxy:       NA 
##     H2O Internal Security:      FALSE 
##     R Version:                  R version 3.3.2 (2016-10-31)
predict.Clinton = as.h2o(Predict_Clinton)
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=================================================================| 100%
vars.Clinton = colnames(predict.Clinton)
x_vars.Clinton = c(vars.Clinton[2:59])
y_var.Clinton = vars.Clinton[1]

Clinton_features = h2o.randomForest(x=x_vars.Clinton,
                                y=y_var.Clinton,
                                seed=123,
                                training_frame = predict.Clinton,
                                ntrees=200,
                                stopping_rounds = 2,
                                score_each_iteration = TRUE,
                                nfolds = 10)
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=                                                                |   2%
  |                                                                       
  |========================                                         |  37%
  |                                                                       
  |==========================================                       |  65%
  |                                                                       
  |================================================                 |  74%
  |                                                                       
  |===========================================================      |  91%
  |                                                                       
  |=================================================================| 100%
summary(Clinton_features)
## Model Details:
## ==============
## 
## H2ORegressionModel: drf
## Model Key:  DRF_model_R_1498093468665_11 
## Model Summary: 
##   number_of_trees number_of_internal_trees model_size_in_bytes min_depth
## 1              21                       21              506918        20
##   max_depth mean_depth min_leaves max_leaves mean_leaves
## 1        20   20.00000       1861       1975  1914.47620
## 
## H2ORegressionMetrics: drf
## ** Reported on training data. **
## ** Metrics reported on Out-Of-Bag training samples **
## 
## MSE:  6.967564
## RMSE:  2.639614
## MAE:  1.987007
## RMSLE:  0.09819153
## Mean Residual Deviance :  6.967564
## 
## 
## 
## H2ORegressionMetrics: drf
## ** Reported on cross-validation data. **
## ** 10-fold cross-validation on training data (Metrics computed for combined holdout predictions) **
## 
## MSE:  6.039764
## RMSE:  2.457593
## MAE:  1.84011
## RMSLE:  0.09185895
## Mean Residual Deviance :  6.039764
## 
## 
## Cross-Validation Metrics Summary: 
##                          mean           sd  cv_1_valid  cv_2_valid
## mae                 1.8428727   0.07523045   1.8915086   1.7472016
## mse                 6.0550523   0.55579835    6.005443   5.3264985
## r2                  0.9741264  0.002257016  0.97279286   0.9773105
## residual_deviance   6.0550523   0.55579835    6.005443   5.3264985
## rmse                2.4556847   0.11105085   2.4506006   2.3079207
## rmsle             0.091426335 0.0060151466 0.093521066 0.091158554
##                   cv_3_valid cv_4_valid cv_5_valid cv_6_valid cv_7_valid
## mae                1.8171152  1.8222564  1.7392036  1.6625148  1.9629086
## mse                 5.776114  5.5907035   5.639608   5.032604  6.8387527
## r2                 0.9789275 0.97289354 0.97356176 0.97540796   0.970742
## residual_deviance   5.776114  5.5907035   5.639608   5.032604  6.8387527
## rmse               2.4033546   2.364467   2.374786  2.2433467  2.6151009
## rmsle             0.08283377 0.07795417 0.08026096  0.0945025 0.09372223
##                   cv_8_valid cv_9_valid cv_10_valid
## mae                1.8111181   1.980861   1.9940398
## mse                 5.645194    7.41484   7.2807646
## r2                 0.9784077  0.9684981   0.9727218
## residual_deviance   5.645194    7.41484   7.2807646
## rmse               2.3759618  2.7230203   2.6982892
## rmsle             0.10725975 0.09982335  0.09322696
## 
## Scoring History: 
##             timestamp   duration number_of_trees training_rmse
## 1 2017-06-22 00:32:13 12.332 sec               0              
## 2 2017-06-22 00:32:13 12.456 sec               1       4.56398
## 3 2017-06-22 00:32:13 12.580 sec               2       4.18082
## 4 2017-06-22 00:32:13 12.694 sec               3       3.94459
## 5 2017-06-22 00:32:14 12.815 sec               4       3.79623
##   training_mae training_deviance
## 1                               
## 2      3.24738          20.82995
## 3      2.99978          17.47927
## 4      2.87190          15.55976
## 5      2.79030          14.41133
## 
## ---
##              timestamp   duration number_of_trees training_rmse
## 17 2017-06-22 00:32:15 14.089 sec              16       2.74439
## 18 2017-06-22 00:32:15 14.183 sec              17       2.71752
## 19 2017-06-22 00:32:15 14.282 sec              18       2.69444
## 20 2017-06-22 00:32:15 14.370 sec              19       2.65668
## 21 2017-06-22 00:32:15 14.455 sec              20       2.64683
## 22 2017-06-22 00:32:15 14.547 sec              21       2.63961
##    training_mae training_deviance
## 17      2.06025           7.53168
## 18      2.04409           7.38492
## 19      2.03443           7.26000
## 20      2.00742           7.05795
## 21      1.99804           7.00569
## 22      1.98701           6.96756
## 
## Variable Importances: (Extract with `h2o.varimp`) 
## =================================================
## 
## Variable Importances: 
##                                      variable relative_importance
## 1                                       Obama      4839851.500000
## 2                                      Romney      3895734.000000
## 3 Accommodation and Food Service Sales - 2007       643884.750000
## 4      Housing Units in Multi-Unit Structures       403044.406250
## 5                                       Black       374463.468750
##   scaled_importance percentage
## 1          1.000000   0.399807
## 2          0.804928   0.321816
## 3          0.133038   0.053190
## 4          0.083276   0.033294
## 5          0.077371   0.030933
## 
## ---
##                          variable relative_importance scaled_importance
## 53                         Mormon         4044.271240          0.000836
## 54 Manufacturers Shipments - 2007         2623.896729          0.000542
## 55                       Orthodox         2301.390381          0.000476
## 56                        Islamic         1621.955444          0.000335
## 57                       Buddhist          752.422119          0.000155
## 58                          Hindu          564.204590          0.000117
##    percentage
## 53   0.000334
## 54   0.000217
## 55   0.000190
## 56   0.000134
## 57   0.000062
## 58   0.000047
h2o.varimp(Clinton_features)[1:20,]
## Variable Importances: 
##                                        variable relative_importance
## 1                                         Obama      4839851.500000
## 2                                        Romney      3895734.000000
## 3   Accommodation and Food Service Sales - 2007       643884.750000
## 4        Housing Units in Multi-Unit Structures       403044.406250
## 5                                         Black       374463.468750
## 6                                         White       316123.406250
## 7                    Private Nonfarm Employment       311307.937500
## 8                             Black-Owned Firms       212851.578125
## 9                                       Density       189867.125000
## 10                                 Foreign Born        97862.742188
## 11                               Edu_batchelors        89795.242188
## 12 Median Value of Owner-Occupied Housing Units        61838.433594
## 13                           Historically_Black        60181.757812
## 14                                   NonEnglish        46723.226562
## 15                        Total Number of Firms        43642.250000
## 16                           Homeownership Rate        33886.480469
## 17                               population2010        30759.291016
## 18                                   Households        22673.718750
## 19                            Persons/Household        22604.105469
## 20                            population_change        21853.570312
##    scaled_importance percentage
## 1           1.000000   0.399807
## 2           0.804928   0.321816
## 3           0.133038   0.053190
## 4           0.083276   0.033294
## 5           0.077371   0.030933
## 6           0.065317   0.026114
## 7           0.064322   0.025716
## 8           0.043979   0.017583
## 9           0.039230   0.015684
## 10          0.020220   0.008084
## 11          0.018553   0.007418
## 12          0.012777   0.005108
## 13          0.012435   0.004971
## 14          0.009654   0.003860
## 15          0.009017   0.003605
## 16          0.007002   0.002799
## 17          0.006355   0.002541
## 18          0.004685   0.001873
## 19          0.004670   0.001867
## 20          0.004515   0.001805
h2o.varimp_plot(Clinton_features, num_of_features = 20)

Trump

predict.Trump = as.h2o(Predict_Trump)
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=================================================================| 100%
vars.Trump = colnames(predict.Trump)
x_vars.Trump = c(vars.Trump[2:59])
y_var.Trump = vars.Trump[1]

Trump_features = h2o.randomForest(x=x_vars.Trump,
                                y=y_var.Trump,
                                seed=123,
                                training_frame = predict.Trump,
                                ntrees=200,
                                stopping_rounds = 2,
                                score_each_iteration = TRUE,
                                nfolds = 10)
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=========================                                        |  38%
  |                                                                       
  |===============================================                  |  73%
  |                                                                       
  |================================================                 |  74%
  |                                                                       
  |===========================================================      |  91%
  |                                                                       
  |=================================================================| 100%
summary(Trump_features)
## Model Details:
## ==============
## 
## H2ORegressionModel: drf
## Model Key:  DRF_model_R_1498093468665_12 
## Model Summary: 
##   number_of_trees number_of_internal_trees model_size_in_bytes min_depth
## 1              18                       18              432639        20
##   max_depth mean_depth min_leaves max_leaves mean_leaves
## 1        20   20.00000       1844       1938  1906.88890
## 
## H2ORegressionMetrics: drf
## ** Reported on training data. **
## ** Metrics reported on Out-Of-Bag training samples **
## 
## MSE:  9.19168
## RMSE:  3.031778
## MAE:  2.273826
## RMSLE:  0.0644703
## Mean Residual Deviance :  9.19168
## 
## 
## 
## H2ORegressionMetrics: drf
## ** Reported on cross-validation data. **
## ** 10-fold cross-validation on training data (Metrics computed for combined holdout predictions) **
## 
## MSE:  8.099768
## RMSE:  2.846009
## MAE:  2.0923
## RMSLE:  0.06425957
## Mean Residual Deviance :  8.099768
## 
## 
## Cross-Validation Metrics Summary: 
##                         mean           sd  cv_1_valid  cv_2_valid
## mae                2.0925953   0.08323233    2.084904   1.9466763
## mse                 8.099447    0.7257394    7.811906    6.649725
## r2                0.96660876 0.0033785722    0.966138   0.9721209
## residual_deviance   8.099447    0.7257394    7.811906    6.649725
## rmse               2.8401918   0.12798046   2.7949786    2.578706
## rmsle             0.06314858  0.009248693 0.055708043 0.050057102
##                    cv_3_valid cv_4_valid cv_5_valid  cv_6_valid
## mae                 1.9816947  1.9424402  1.9840581    2.267307
## mse                  7.452567  6.5643163   7.939525    9.761651
## r2                 0.97353655   0.969534 0.96519685  0.95566523
## residual_deviance    7.452567  6.5643163   7.939525    9.761651
## rmse                 2.729939   2.562092  2.8177164   3.1243641
## rmsle             0.055192083 0.05458503 0.07344787 0.060759306
##                    cv_7_valid  cv_8_valid cv_9_valid cv_10_valid
## mae                  2.107805    2.161643    2.21629   2.2331357
## mse                  7.728803    9.073089   8.840077    9.172812
## r2                  0.9676459   0.9672588  0.9623796  0.96661174
## residual_deviance    7.728803    9.073089   8.840077    9.172812
## rmse                2.7800725   3.0121567  2.9732268    3.028665
## rmsle             0.061720204 0.058766253   0.063254  0.09799592
## 
## Scoring History: 
##              timestamp   duration number_of_trees training_rmse
## 1  2017-06-22 00:32:28 11.351 sec               0              
## 2  2017-06-22 00:32:28 11.466 sec               1       4.68151
## 3  2017-06-22 00:32:29 11.578 sec               2       4.46339
## 4  2017-06-22 00:32:29 11.683 sec               3       4.18316
## 5  2017-06-22 00:32:29 11.793 sec               4       4.16084
## 6  2017-06-22 00:32:29 11.905 sec               5       3.93286
## 7  2017-06-22 00:32:29 12.011 sec               6       3.75325
## 8  2017-06-22 00:32:29 12.121 sec               7       3.64086
## 9  2017-06-22 00:32:29 12.237 sec               8       3.58072
## 10 2017-06-22 00:32:29 12.388 sec               9       3.52243
## 11 2017-06-22 00:32:29 12.500 sec              10       3.44681
## 12 2017-06-22 00:32:30 12.607 sec              11       3.37018
## 13 2017-06-22 00:32:30 12.716 sec              12       3.27858
## 14 2017-06-22 00:32:30 12.829 sec              13       3.22463
## 15 2017-06-22 00:32:30 12.944 sec              14       3.17985
## 16 2017-06-22 00:32:30 13.060 sec              15       3.13531
## 17 2017-06-22 00:32:30 13.161 sec              16       3.09054
## 18 2017-06-22 00:32:30 13.259 sec              17       3.05684
## 19 2017-06-22 00:32:30 13.351 sec              18       3.03178
##    training_mae training_deviance
## 1                                
## 2       3.50252          21.91649
## 3       3.33913          19.92182
## 4       3.13500          17.49880
## 5       3.05750          17.31255
## 6       2.91188          15.46737
## 7       2.78487          14.08689
## 8       2.70936          13.25584
## 9       2.66056          12.82155
## 10      2.61323          12.40750
## 11      2.56129          11.88051
## 12      2.51101          11.35811
## 13      2.44729          10.74907
## 14      2.40585          10.39823
## 15      2.35768          10.11145
## 16      2.34510           9.83017
## 17      2.31388           9.55142
## 18      2.29450           9.34427
## 19      2.27383           9.19168
## 
## Variable Importances: (Extract with `h2o.varimp`) 
## =================================================
## 
## Variable Importances: 
##                                      variable relative_importance
## 1                                      Romney      5155287.000000
## 2                                       Obama      2963044.500000
## 3      Housing Units in Multi-Unit Structures       528261.750000
## 4 Accommodation and Food Service Sales - 2007       367348.062500
## 5                              Edu_batchelors       318496.687500
##   scaled_importance percentage
## 1          1.000000   0.471791
## 2          0.574758   0.271166
## 3          0.102470   0.048344
## 4          0.071257   0.033618
## 5          0.061781   0.029148
## 
## ---
##                          variable relative_importance scaled_importance
## 53 Manufacturers Shipments - 2007         4126.576660          0.000800
## 54                       Orthodox         1809.172729          0.000351
## 55                         Jewish         1600.463379          0.000310
## 56                        Islamic         1317.801880          0.000256
## 57                       Buddhist          751.266907          0.000146
## 58                          Hindu           88.646675          0.000017
##    percentage
## 53   0.000378
## 54   0.000166
## 55   0.000146
## 56   0.000121
## 57   0.000069
## 58   0.000008
h2o.varimp(Trump_features)[1:20,]
## Variable Importances: 
##                                        variable relative_importance
## 1                                        Romney      5155287.000000
## 2                                         Obama      2963044.500000
## 3        Housing Units in Multi-Unit Structures       528261.750000
## 4   Accommodation and Food Service Sales - 2007       367348.062500
## 5                                Edu_batchelors       318496.687500
## 6                                         Black       273496.593750
## 7           Private Nonfarm Establishments 2013       187035.359375
## 8                    Private Nonfarm Employment       159617.578125
## 9  Median Value of Owner-Occupied Housing Units        93521.976562
## 10                                        White        83486.484375
## 11                                 Foreign Born        82496.109375
## 12                            Black-Owned Firms        54077.496094
## 13                                  Evangelical        53287.820312
## 14                                   NonEnglish        47489.898438
## 15                           Historically_Black        36577.964844
## 16                               population2010        29904.724609
## 17                            population_change        28675.873047
## 18                            Persons/Household        25468.931641
## 19                                     Hispanic        25454.554688
## 20                           Housing Units 2014        24591.384766
##    scaled_importance percentage
## 1           1.000000   0.471791
## 2           0.574758   0.271166
## 3           0.102470   0.048344
## 4           0.071257   0.033618
## 5           0.061781   0.029148
## 6           0.053052   0.025029
## 7           0.036280   0.017117
## 8           0.030962   0.014608
## 9           0.018141   0.008559
## 10          0.016194   0.007640
## 11          0.016002   0.007550
## 12          0.010490   0.004949
## 13          0.010337   0.004877
## 14          0.009212   0.004346
## 15          0.007095   0.003347
## 16          0.005801   0.002737
## 17          0.005562   0.002624
## 18          0.004940   0.002331
## 19          0.004938   0.002329
## 20          0.004770   0.002251
h2o.varimp_plot(Trump_features, num_of_features = 20)

Swing Predict

predict.Swing = as.h2o(Overall_Dev_Predict)
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=================================================================| 100%
vars.Swing = colnames(predict.Swing)
x_vars.Swing = c(vars.Swing[1:60],vars.Swing[62])
y_var.Swing = vars.Swing[61]

Swing_features = h2o.randomForest(x=x_vars.Swing,
                                y=y_var.Swing,
                                seed=123,
                                training_frame = predict.Swing,
                                ntrees=200,
                                stopping_rounds = 2,
                                score_each_iteration = TRUE,
                                nfolds = 10)
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |==                                                               |   2%
  |                                                                       
  |=============                                                    |  20%
  |                                                                       
  |====================================                             |  56%
  |                                                                       
  |================================================                 |  74%
  |                                                                       
  |======================================================           |  82%
  |                                                                       
  |===========================================================      |  91%
  |                                                                       
  |============================================================     |  92%
  |                                                                       
  |=================================================================| 100%
summary(Swing_features)
## Model Details:
## ==============
## 
## H2ORegressionModel: drf
## Model Key:  DRF_model_R_1498093468665_13 
## Model Summary: 
##   number_of_trees number_of_internal_trees model_size_in_bytes min_depth
## 1              20                       20              482482        20
##   max_depth mean_depth min_leaves max_leaves mean_leaves
## 1        20   20.00000       1866       1988  1912.80000
## 
## H2ORegressionMetrics: drf
## ** Reported on training data. **
## ** Metrics reported on Out-Of-Bag training samples **
## 
## MSE:  23.38624
## RMSE:  4.835932
## MAE:  3.640499
## RMSLE:  NaN
## Mean Residual Deviance :  23.38624
## 
## 
## 
## H2ORegressionMetrics: drf
## ** Reported on cross-validation data. **
## ** 10-fold cross-validation on training data (Metrics computed for combined holdout predictions) **
## 
## MSE:  20.82928
## RMSE:  4.563911
## MAE:  3.454607
## RMSLE:  NaN
## Mean Residual Deviance :  20.82928
## 
## 
## Cross-Validation Metrics Summary: 
##                        mean          sd cv_1_valid cv_2_valid cv_3_valid
## mae                3.456276  0.11210502  3.5003939  3.2188752  3.1686027
## mse               20.833815   1.5101509   21.30881  18.320068   17.43827
## r2                0.7998607 0.015021347  0.7985061 0.79740304  0.8198827
## residual_deviance 20.833815   1.5101509   21.30881  18.320068   17.43827
## rmse                4.55834  0.16635615  4.6161466  4.2801948  4.1759157
## rmsle                   0.0         NaN        NaN        NaN        NaN
##                   cv_4_valid cv_5_valid cv_6_valid cv_7_valid cv_8_valid
## mae                3.3518422   3.576076   3.601392   3.483406  3.4794314
## mse                 18.83481  22.753336  23.270376  21.153341  22.000587
## r2                 0.8184314 0.77076167  0.7945231    0.77902  0.8145726
## residual_deviance   18.83481  22.753336  23.270376  21.153341  22.000587
## rmse                4.339909  4.7700458   4.823938   4.599276  4.6904783
## rmsle                    NaN        NaN        NaN        NaN        NaN
##                   cv_9_valid cv_10_valid
## mae                3.7058234   3.4769158
## mse                23.994698   19.263847
## r2                0.76946336  0.83604324
## residual_deviance  23.994698   19.263847
## rmse               4.8984385     4.38906
## rmsle                    NaN         NaN
## 
## Scoring History: 
##             timestamp   duration number_of_trees training_rmse
## 1 2017-06-22 00:32:45 12.823 sec               0              
## 2 2017-06-22 00:32:45 12.945 sec               1       6.80282
## 3 2017-06-22 00:32:45 13.076 sec               2       6.73403
## 4 2017-06-22 00:32:45 13.200 sec               3       6.47733
## 5 2017-06-22 00:32:45 13.320 sec               4       6.35175
##   training_mae training_deviance
## 1                               
## 2      5.16590          46.27842
## 3      5.04244          45.34712
## 4      4.85260          41.95584
## 5      4.70634          40.34476
## 
## ---
##              timestamp   duration number_of_trees training_rmse
## 16 2017-06-22 00:32:47 14.662 sec              15       4.98087
## 17 2017-06-22 00:32:47 14.778 sec              16       4.95881
## 18 2017-06-22 00:32:47 14.890 sec              17       4.93672
## 19 2017-06-22 00:32:47 15.003 sec              18       4.89476
## 20 2017-06-22 00:32:47 15.114 sec              19       4.86762
## 21 2017-06-22 00:32:47 15.222 sec              20       4.83593
##    training_mae training_deviance
## 16      3.74797          24.80909
## 17      3.73866          24.58977
## 18      3.71992          24.37123
## 19      3.69023          23.95865
## 20      3.66254          23.69370
## 21      3.64050          23.38624
## 
## Variable Importances: (Extract with `h2o.varimp`) 
## =================================================
## 
## Variable Importances: 
##            variable relative_importance scaled_importance percentage
## 1      Foreign Born       780725.812500          1.000000   0.168096
## 2    Edu_batchelors       767971.062500          0.983663   0.165350
## 3            Romney       293125.968750          0.375453   0.063112
## 4             Obama       287345.687500          0.368049   0.061868
## 5 population_change       264071.625000          0.338239   0.056857
## 
## ---
##          variable relative_importance scaled_importance percentage
## 56 population2010         6572.494629          0.008418   0.001415
## 57       Orthodox         5954.307129          0.007627   0.001282
## 58         Jewish         3808.336426          0.004878   0.000820
## 59       Buddhist         3445.218506          0.004413   0.000742
## 60        Islamic         2736.301758          0.003505   0.000589
## 61          Hindu         1201.396729          0.001539   0.000259
h2o.varimp(Swing_features)[1:20,]
## Variable Importances: 
##                                        variable relative_importance
## 1                                  Foreign Born       780725.812500
## 2                                Edu_batchelors       767971.062500
## 3                                        Romney       293125.968750
## 4                                         Obama       287345.687500
## 5                             population_change       264071.625000
## 6                                     age65plus       191607.312500
## 7                              Building Permits       176082.515625
## 8                                         Black       170653.609375
## 9                                         White       143307.312500
## 10                                   NonEnglish       135277.156250
## 11                            Persons/Household        91002.992188
## 12 Median Value of Owner-Occupied Housing Units        88411.570312
## 13                                     Hispanic        88308.492188
## 14                                       Mormon        74074.476562
## 15                                  Evangelical        68271.750000
## 16                                    Christian        66716.500000
## 17                           Historically_Black        65353.214844
## 18                                   Protestant        53810.617188
## 19                             Persons Under 18        49755.976562
## 20                                       Income        45096.437500
##    scaled_importance percentage
## 1           1.000000   0.168096
## 2           0.983663   0.165350
## 3           0.375453   0.063112
## 4           0.368049   0.061868
## 5           0.338239   0.056857
## 6           0.245422   0.041254
## 7           0.225537   0.037912
## 8           0.218583   0.036743
## 9           0.183557   0.030855
## 10          0.173271   0.029126
## 11          0.116562   0.019594
## 12          0.113243   0.019036
## 13          0.113111   0.019013
## 14          0.094879   0.015949
## 15          0.087447   0.014699
## 16          0.085454   0.014365
## 17          0.083708   0.014071
## 18          0.068924   0.011586
## 19          0.063730   0.010713
## 20          0.057762   0.009710
h2o.varimp_plot(Swing_features, num_of_features = 20)

Correlation of Most important swing variables

corr_subset_swing = votes[,c(8,9,43,46,20,19,28,32,74,35,34,44,54)]

correlation_swing = cor(corr_subset_swing,use = "complete.obs")


swing = corrplot(correlation_swing, method="shade", shade.col=NA, tl.col="black", tl.srt=45, addCoef.col="black", addcolorlabel="no")
## Warning in text.default(pos.xlabel[, 1], pos.xlabel[, 2], newcolnames, srt
## = tl.srt, : "addcolorlabel" is not a graphical parameter
## Warning in text.default(pos.ylabel[, 1], pos.ylabel[, 2], newrownames, col
## = tl.col, : "addcolorlabel" is not a graphical parameter
## Warning in title(title, ...): "addcolorlabel" is not a graphical parameter

swing
##                      Clinton      Trump Foreign Born Edu_batchelors
## Clinton            1.0000000 -0.9837064  0.391563486     0.43422424
## Trump             -0.9837064  1.0000000 -0.395191097    -0.48724875
## Foreign Born       0.3915635 -0.3951911  1.000000000     0.36658290
## Edu_batchelors     0.4342242 -0.4872487  0.366582898     1.00000000
## Romney            -0.9415098  0.9342740 -0.242001812    -0.30002733
## Obama              0.9466969 -0.9343402  0.244280092     0.29265640
## population_change  0.1160021 -0.1438740  0.316927722     0.43580623
## age65plus         -0.3078179  0.3230607 -0.344869443    -0.23498620
## Building Permits   0.2819284 -0.2868319  0.420037480     0.34895577
## Black              0.5092690 -0.4251729  0.009439359    -0.08473178
## White             -0.5930490  0.5296621 -0.111214215    -0.00106647
## NonEnglish         0.3203200 -0.3264593  0.822902882     0.15263309
## Persons/Household  0.1655445 -0.1705746  0.411876494    -0.06228232
##                         Romney       Obama population_change  age65plus
## Clinton           -0.941509781  0.94669692       0.116002093 -0.3078179
## Trump              0.934274000 -0.93434018      -0.143874022  0.3230607
## Foreign Born      -0.242001812  0.24428009       0.316927722 -0.3448694
## Edu_batchelors    -0.300027325  0.29265640       0.435806228 -0.2349862
## Romney             1.000000000 -0.99813021      -0.005088279  0.2017157
## Obama             -0.998130214  1.00000000       0.005120690 -0.2084752
## population_change -0.005088279  0.00512069       1.000000000 -0.4145400
## age65plus          0.201715712 -0.20847523      -0.414539997  1.0000000
## Building Permits  -0.186745154  0.18824115       0.334357078 -0.2307868
## Black             -0.390921003  0.41515417      -0.098876154 -0.2256728
## White              0.478312388 -0.50034927      -0.010339887  0.3126738
## NonEnglish        -0.205383341  0.20735084       0.238685010 -0.3026660
## Persons/Household -0.049336849  0.06191844       0.275078327 -0.6041703
##                   Building Permits        Black       White  NonEnglish
## Clinton                 0.28192841  0.509268989 -0.59304898  0.32031996
## Trump                  -0.28683186 -0.425172945  0.52966212 -0.32645928
## Foreign Born            0.42003748  0.009439359 -0.11121421  0.82290288
## Edu_batchelors          0.34895577 -0.084731778 -0.00106647  0.15263309
## Romney                 -0.18674515 -0.390921003  0.47831239 -0.20538334
## Obama                   0.18824115  0.415154166 -0.50034927  0.20735084
## population_change       0.33435708 -0.098876154 -0.01033989  0.23868501
## age65plus              -0.23078680 -0.225672806  0.31267378 -0.30266597
## Building Permits        1.00000000  0.066066494 -0.12919138  0.28003297
## Black                   0.06606649  1.000000000 -0.87293276 -0.07267724
## White                  -0.12919138 -0.872932756  1.00000000 -0.04805566
## NonEnglish              0.28003297 -0.072677242 -0.04805566  1.00000000
## Persons/Household       0.14228533  0.152645633 -0.30238451  0.50826960
##                   Persons/Household
## Clinton                  0.16554447
## Trump                   -0.17057457
## Foreign Born             0.41187649
## Edu_batchelors          -0.06228232
## Romney                  -0.04933685
## Obama                    0.06191844
## population_change        0.27507833
## age65plus               -0.60417034
## Building Permits         0.14228533
## Black                    0.15264563
## White                   -0.30238451
## NonEnglish               0.50826960
## Persons/Household        1.00000000